- Home /
Duplicate Question
How to combine anim states with movement in a random movement game.
Ok so I am sorry because I don't know how to express my problem in one sentence but here it is:
Im trying to create a 2D top down game. The game will be about a zoo and animals. My main goal here is to create a prefab of an animal and scatter a few of them inside a fence. So what I want to do is for this prefab to wander around the cage doing random movement. The prefab has two states. One idle and one walking. My problem is that while I have found a way(through the community) to create a random way to choose between states and displaying them correctly, my 2D sprite won't move.
enter code here` void Start() {
InvokeRepeating("ExecuteAfterTime", 2, 4);
//yield return StartCoroutine(ExecuteAfterTime(2.0F));
animator = GetComponent<Animator>();
}
void ExecuteAfterTime()
{
{
randvalue = Random.value;
if (Random.value >= 0.5)
{
Idle = true;
}
else
Idle = false;
// change to random direction at random intervals
if (Time.time >= tChange)
{
randomX = Random.Range(-2.0f, 2.0f); // with float parameters, a random float
randomY = Random.Range(-2.0f, 2.0f); // between -2.0f and 2.0f is returned
// set a random interval between 0.5f and 1.5f
tChange = Time.time + Random.Range(0.5f, 1.5f);
}
if (Idle != true)
{
isCoroutineExecuting = true;
transform.Translate(new Vector3(randomX, randomY, 0) * moveSpeed * Time.deltaTime);
animator.SetBool("DinoIdle", false); // DInoIdle is my bool which changes the anim states.
}
else
animator.SetBool("DinoIdle", true);
// if object reached any border, revert the appropriate direction
if (transform.position.x >= maxX || transform.position.x <= minX)
{
randomX = -randomX;
}
if (transform.position.y >= maxY || transform.position.y <= minY)
{
randomY = -randomY;
}
// make sure the position is inside the borders
Vector3 temp = transform.position;
temp.x = Mathf.Clamp(transform.position.x, minX, maxX);
temp.y = Mathf.Clamp(transform.position.y, minY, maxY);
moveSpeed = Random.Range(0.5f, 2.0f);
moveSpeed = Random.Range(0.5f, 2.0f);
isCoroutineExecuting = false;
}
}
void Update()
{
if (isCoroutineExecuting)
{
transform.Translate(new Vector3(randomX, randomY, 0) * moveSpeed * Time.deltaTime);
}
}`
I imagine this is happening since the movement takes place inside the coroutine and not inside the update. But if I put the code of the couroutine inside the update function, then the prefab changes states like crazy( which makes sense ) and ruins what I am trying to achieve. Is there any way I can do this? Am I using the wrong approach? I figured I should use Navmesh but I want to be able to click on an animal and force it to go a predefined route (a single target, not wherever the player wants). My programming skills are not the best but I'm trying. Thank you for reading this, any help will be great.
I think you should go with Nav$$anonymous$$esh and a NavAgent, then you could make it wanter randomly and also point where is should go if you would like.
Quick code to have them walk random with Nav$$anonymous$$esh:
using System.Collections;
using UnityEngine;
[RequireComponent(typeof(Nav$$anonymous$$eshAgent))]
public class AnimalControl : $$anonymous$$onoBehaviour
{
private Nav$$anonymous$$eshAgent _nav$$anonymous$$eshAgent;
public float WalkingRaduis;
private void Start()
{
_nav$$anonymous$$eshAgent = GetComponent<Nav$$anonymous$$eshAgent>(); // Get reference to you Nav$$anonymous$$eshAgent
WanderRandom();
}
private void Update()
{
if (_nav$$anonymous$$eshAgent.remainingDistance < 0.1f || _nav$$anonymous$$eshAgent.desiredVelocity.magnitude < 0.1f)
{
WanderRandom();
}
}
private void WanderRandom()
{
_nav$$anonymous$$eshAgent.SetDestination(FindRandomPoint());
}
private Vector3 FindRandomPoint()
{
var direction = transform.forward;
return direction + Random.insideUnitSphere * WalkingRaduis;
}
}
Answer by NovacainE2 · Nov 10, 2016 at 02:52 PM
Hey @Filhanteraren ! Thank you so much for getting back to me and thanks for sharing the script. I'll do some research now and let you know about the results and also marking this thread as answered once I work on it. However do you have in mind what would it take to to change the animation states in a more discreet way? On my script I use a boolean variable to change between states but the problem that it doesn't work right because the animation must go along the movement part right? If I try to change states once an animal is moving (which happens in update) then the animation states would change every frame. The reason for that is that I am trying to achieve Idle and walking behaviors alike. So my goal is to make an animal wander around, stop, and wander around more in any random order with proper switching between the anim states. Anyway, thanks again, wish you all the best!
Follow this Question
Related Questions
Creating an animation with variables? 2 Answers
Handling Animation and movement speed 0 Answers
Tank Wheels and Treads 1 Answer
scripting for animation 0 Answers