Hi
I’m new to Unity3d scripting, but have been coding for 8 years, so I know my way around C# ![]()
I’m trying to do a steering behavior like the mouse have in “Cyber mice” : Play Cyber Mice Party 2003 3 Online - Free Game - MuchGames.com
They start with random direction and then move forward until they hit an obstacle. Here they turn, until they don’t collide anymore (thats the part I cant figure out how to do) and then move forward again.
I’m using the new 2D colliders here.
I’m asking for help to make a script that can do the above behavior, here’s what I have now, which DOESN’T work exactly as above.
The problem is the rotate until not colliding anymore.
Guess this is a easy one, I just can’t figure out how to do this in Unity3d ![]()
Thanks in advance,
public class WanderMovement : MonoBehaviour
{
public float _speed = 0.4f;
private Animator _anim;
private Transform _transform;
private Vector2 _direction;
private float _rotationAngle = 0f;
private Vector2 _lastPosition;
private bool _isColliding = false;
void Start ()
{
_anim = GetComponent<Animator>();
_transform = transform;
_lastPosition = _transform.position;
//Start moving in random direction
_direction = new Vector2(Random.Range(-1.0f, 1.0f),
Random.Range(-1.0f, 1.0f));
_direction.Normalize();
}
// Update is called once per frame
void Update ()
{
_transform.Translate(_direction * _speed * Time.deltaTime);
if(_isColliding == false)
_lastPosition = _transform.position;
_anim.SetFloat("DirectionY", _direction.y);
_isColliding = false;
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Obstacle")
{
_isColliding = true;
_transform.position = _lastPosition;
}
}
void OnTriggerStay2D(Collider2D other)
{
if(other.tag == "Obstacle")
{
_isColliding = true;
_transform.position = _lastPosition;
_rotationAngle += 0.001f;
_direction = Quaternion.AngleAxis(_rotationAngle, Vector3.forward) * _direction;
Debug.Log(_rotationAngle);
}
}
}
UPDATE:
I see I forgot to mention an important think, here goes:
My “mice” doesn’t rotate as the mice in “Cyber mice”, at least not visually like the mice in “Cyber mice”. Their direction change, but their rotation is not. Think 2.5D spritesheet, where you have sprites to move up,down,right,left without the image/sprite rotating.
Here’s an sample of what I mean:
