Unity RPG Tutorial #11 - Making Enemies

preview_player
Показать описание
Learn how to make your own RPG game using Unity! In this episode we're going to add some enemies to the world and let them move around in it!

Get the scripts used in this series by supporting on Patreon at:

Don't forget to hit that like button and if you'd like to see more gaming goodness then subscribe for more!

And you can also find me on twitter and facebook!

Рекомендации по теме
Комментарии
Автор

how to make an enemy 101.
1. find someone you don't really like
2.be pretty rude to him
3.there you go you made an enemy ;D
(btw love the videos, keep it up!)

lime
Автор

For those who are unable to get the code to work, copy and paste this. (Its the exact same as on the video so you don't have to error hunt)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SlimeController : MonoBehaviour
{

public float moveSpeed;

private Rigidbody2D myRigidbody;

private bool moving;

public float timeBetweenMove;
private float timeBetweenMoveCounter;

public float timeToMove;
private float timeToMoveCounter;

private Vector3 moveDirection;

// Use this for initialization
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();

timeBetweenMoveCounter = timeBetweenMove;
timeToMoveCounter = timeToMove;
}

// Update is called once per frame
void Update()
{
if (moving)
{
timeToMoveCounter -= Time.deltaTime;
myRigidbody.velocity = moveDirection;

if (timeToMoveCounter < 0f)
{
moving = false;
timeBetweenMoveCounter = timeBetweenMove;
}

}
else
{
timeBetweenMoveCounter -= Time.deltaTime;
myRigidbody.velocity = Vector2.zero;
if (timeBetweenMoveCounter < 0f)
{
moving = true;
timeToMoveCounter = timeToMove;

moveDirection = new Vector3(Random.Range(-1f, 1f)* moveSpeed, Random.Range(-1f, 1f) * moveSpeed, 0f);
}
}
}
}

WWatson
Автор

Good stuff!

I added a little tweak to the (moving) conditionals to increase / decrease the animation speed depending on if the enemy is moving, so it gives a better effect when the enemy is idle versus moving around:

private Animator animator;

void Start()
{
// Get the animator
animator = GetComponent<Animator>();
}

if(moving) {
/* ... your other code */

// Set animation speed (to simulate moving)
animator.speed = 2; // Doubles animation speed
} else {
/* ... your other code */
// Set animation speed (to simulate being idle)
animator.speed = 1; // Sets animation speed back to normal
}

HeyRay
Автор

It makes me so excited to see moving enemies in my game! :D

suicune
Автор

If you enemy isn't moving? Click "Slime_movement" in your Animations folder and in Inspector make sure "Loop pose" is checked. That may help. If not, sorry. :)

djdavidj
Автор

Subscribed. Even though I'm willing to do something more 3d, I feel like this tutorial will have all the knowledge I need about stats and player-environment(and enemies) interaction.

DarthKamci
Автор

Don't get me wrong. This is great, much appreciated, but dude is dehydrated. Swallowing his spit just trying to stay alive. Get this man a drink stat.

jensindalmout
Автор

Hey guys if any of you need the full code here it is the one he uses in his video most of you are having trouble with it this one should work tell me if it does using UnityEngine;
using System.Collections;

public class SlimeController : MonoBehaviour
{

public float moveSpeed;
public float timeBetweenMove;
private float timeBetweenMoveCounter;
public float timeToMove;
private float timeToMoveCounter;
private Rigidbody2D myrigid;
private bool moving;
private Vector3 moveDirection;

void Start()
{
myrigid = GetComponent<Rigidbody2D>();

//timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
//timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeToMove * 1.25f);

timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeBetweenMove * 1.25f);

}
void Update()
{
if (moving)
{
timeToMoveCounter -= Time.deltaTime;
myrigid.velocity = moveDirection;

if (timeToMoveCounter < 0f)
{
moving = false;
timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
}
}
else
{
timeBetweenMoveCounter -= Time.deltaTime;
myrigid.velocity = Vector2.zero;
if (timeBetweenMoveCounter < 0f)
{
moving = true;
timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeToMove * 1.25f);

moveDirection = new Vector3(Random.Range(-1f, 1f) * moveSpeed, Random.Range(-1f, 1f) * moveSpeed, 0f);
}

}
}
}

Ximithie
Автор

Wow, it takes so much work to implement every single feature that a RPG has that simply comes out of the box in RPGMaker. Of course, you have a lot of freedom in Unity, but it comes at a high price: lower development speed for simple things (character animation and moving, creating enemies, etc)

felipedacruz
Автор

I basically duplicated your scripts and physics settings, but my player and slimes still slide a bit around the place. Anyone got any tips?

wyrmoffastring
Автор

Nice one :)
Just out of curiousity, are we going to give these slimes some kind of agro/go towards the player/attack ai later on? Or are they more a sort of kind of baddie? :d

frankeeeej
Автор

Enemy isn't moving but I have no compile errors? any thoughts?

EDIT: Managed to get it to work, don't know what happened all I did was close Unity and restart and for some odd reason it works now.

Only issue now is the monster will only go in one direction and keep trying to head in that direction even when it hits a wall.

marcuslane
Автор

My enemy moves once, but then just hangs out in one spot. Anybody have any advice?

acoolguyonacorner
Автор

how would I script my Ai If I do use blend trees similar to the character model? wish you could of elaborated on that more.

Tunlor
Автор

Thanks for the awesome videos!!!I have a problem with the slimes!They keep sliding towards the direction of their first move even though i put the gravity scale to 0!Any suggestions?

Vasonist
Автор

I've written the script exactly the way you have but my enemy does not move when the game is played. I used a Debug.Log the see if the if and else statements were being called but it turns out they aren't. I don't know what the problem could be!

borgestheborg
Автор

Would a coroutine be better here? I got my slimes moving around in a simple Coroutine driven FSM. Give it randomized wait times between the Move state and the Recover state then did similar random directoin movement in the Move state.

MatthewCaldwell
Автор

Awesome :) Are you planning on adding an RPG Statistics system to this project?

lvx
Автор

Hello, your tutorials are very helpful and I know that it is "finished" but can you make an animated enemy with walking, idle and attack animations like the player because thats exactly my problem and nobody on youtube did it

sorry for spelling mistakes I´m german

iseeyou
Автор

Ha! I had made some sprites from before watching this tutorial... and one of them happened to be a red slime.

Gandalf_the_Black_
visit shbcf.ru