Question by
lesterb22 · Sep 06, 2019 at 03:28 AM ·
2d game2d animation2d platformer
Character Stop moving randomly when animation is added.
Hello there, I have been working on a 2d game and bump into this problem.
My script makes my character move randomly on waypoints that I set. However, when I put animation to my character it stop from moving randomly. Instead, the animation only plays but my character has stop moving around the map.
You can see here is my code. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CatControl : MonoBehaviour {
[SerializeField]
Transform[] waypoints;
//new
Animator anim;
Rigidbody2D player;
//new
float moveSpeed = 2f;
int waypointIndex = 0;
public float newposition = 0.0f;
void Start () {
transform.position = waypoints [waypointIndex].transform.position;
//new
anim = GetComponent<Animator>();
player = GetComponent<Rigidbody2D>();
newposition = transform.position.x;
}
void Update () {
//new
if (transform.position.x > newposition)
{
print("I am going right");
anim.SetBool("Ismovingleft", true);
}
if (transform.position.x < newposition)
{
print("I am going left");
anim.SetBool("Ismovingright", true);
}
newposition = transform.position.x;
Move();
}
void Move()
{
transform.position = Vector2.MoveTowards (transform.position,
waypoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime);
if (transform.position == waypoints [waypointIndex].transform.position) {
int index = Random.Range(0, waypoints.Length);
waypointIndex = index;
}
if (waypointIndex == waypoints.Length)
waypointIndex = 0;
}
}
Here is a video of my character moving randomly without animation. https://streamable.com/xa240 with Animation. https://streamable.com/rgww6
Here is also my animator controller and preset.
Need help it doesnt move.
pic1byg.png
(189.4 kB)
pic1byg2.png
(185.6 kB)
Comment