- Home /
 
               Question by 
               aakashs434 · Feb 07, 2020 at 08:16 AM · 
                scripting problemmovementscripting beginnerbeginneranimator controller  
              
 
              I Have made a movement script for my pacman sprite. But animations keep playing even when there is no input. ans when i drag my sprite around in the scene it keeps trying to return to its original position.
 public class PacmanMove : MonoBehaviour
 {
     public float speed = 0.4f;
     Vector2 destination = Vector2.zero;
     // used for initalisation
     void Start()
     {
         destination = transform.position;
         
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {//move closer to the set destination at the speed that is set
         Vector2 p = Vector2.MoveTowards(transform.position, destination, speed);
         //set variable p as a vector to show where to move at what speed
         GetComponent<Rigidbody2D>().MovePosition(p);
         // use that variable to move pacman in said direction at said speed
         //check if there is an input at regular intervals
         if ((Vector2)transform.position == destination)
         {//if there is an input of the arrow keys move pacman in that direction by 1 unit
             if (Input.GetKey(KeyCode.UpArrow) && valid(Vector2.up))
                 destination = (Vector2)transform.position + Vector2.up;
             if (Input.GetKey(KeyCode.RightArrow) && valid(Vector2.right))
                 destination = (Vector2)transform.position + Vector2.right;
             if (Input.GetKey(KeyCode.DownArrow) && valid(-Vector2.up))
                 destination = (Vector2)transform.position - Vector2.up;
             if (Input.GetKey(KeyCode.LeftArrow) && valid(-Vector2.right))
                 destination = (Vector2)transform.position - Vector2.right;
         }//changes the dir x and dir y variables to allow the animation to change when different 
         //arrow keys are pressed
         Vector2 dir = destination - (Vector2)transform.position;
         GetComponent<Animator>().SetFloat("dirx", dir.x);
         GetComponent<Animator>().SetFloat("diry", dir.y);
 
     }
 
     bool valid(Vector2 dir)
     {//make a line from next to pacman to pacman to see
         //if there are any obstructions to stop him from moving e.g.(wall)
         Vector2 pos = transform.position;
         RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
         return (hit.collider == GetComponent<Collider2D>());
 
     }
 }   
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                