Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

When I move my character it goes straight back to where it started??

Discussion in 'Scripting' started by CoopReed, Dec 11, 2015.

  1. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    When I move my character it goes striaght back to where it started??

    Please help me here is my code
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public float speed = 6f;            // The speed that the player will move at.
    6.  
    7.     Vector3 movement;                   // The vector to store the direction of the player's movement.
    8.     Animator anim;                      // Reference to the animator component.
    9.     Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
    10.     int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
    11.     float camRayLength = 100f;          // The length of the ray from the camera into the scene.
    12.  
    13.     void Awake()
    14.     {
    15.         // Create a layer mask for the floor layer.
    16.         floorMask = LayerMask.GetMask("Floor");
    17.  
    18.         // Set up references.
    19.         anim = GetComponent<Animator>();
    20.         playerRigidbody = GetComponent<Rigidbody>();
    21.     }
    22.  
    23.  
    24.     void FixedUpdate()
    25.     {
    26.         // Store the input axes.
    27.         float h = Input.GetAxisRaw("Horizontal");
    28.         float v = Input.GetAxisRaw("Vertical");
    29.  
    30.         // Move the player around the scene.
    31.         Move(h, v);
    32.  
    33.         // Turn the player to face the mouse cursor.
    34.         Turning();
    35.  
    36.         // Animate the player.
    37.         Animating(h, v);
    38.     }
    39.  
    40.     void Move(float h, float v)
    41.     {
    42.         // Set the movement vector based on the axis input.
    43.         movement.Set(h, 0f, v);
    44.  
    45.         // Normalise the movement vector and make it proportional to the speed per second.
    46.         movement = movement.normalized * speed * Time.deltaTime;
    47.  
    48.         // Move the player to it's current position plus the movement.
    49.         playerRigidbody.MovePosition(transform.position + movement);
    50.     }
    51.  
    52.     void Turning()
    53.     {
    54.         // Create a ray from the mouse cursor on screen in the direction of the camera.
    55.         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    56.  
    57.         // Create a RaycastHit variable to store information about what was hit by the ray.
    58.         RaycastHit floorHit;
    59.  
    60.         // Perform the raycast and if it hits something on the floor layer...
    61.         if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
    62.         {
    63.             // Create a vector from the player to the point on the floor the raycast from the mouse hit.
    64.             Vector3 playerToMouse = floorHit.point - transform.position;
    65.  
    66.             // Ensure the vector is entirely along the floor plane.
    67.             playerToMouse.y = 0f;
    68.  
    69.             // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
    70.             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
    71.  
    72.             // Set the player's rotation to this new rotation.
    73.             playerRigidbody.MoveRotation(newRotation);
    74.         }
    75.     }
    76.  
    77.     void Animating(float h, float v)
    78.     {
    79.         // Create a boolean that is true if either of the input axes is non-zero.
    80.         bool walking = h != 0f || v != 0f;
    81.  
    82.         // Tell the animator whether or not the player is walking.
    83.         anim.SetBool("IsWalking", walking);
    84.     }
    85. }
    86.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    as in you stop moving it goes back to where it was? what do you mean by "straight back"?
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    Does it look like the character moves forward 2 units and then, after playing one cycle of the walk loop, it pops back 1 unit? If so, the animation has root motion. In this case, don't move the character in your script; let root motion handle movement.
     
  4. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    Okay sorry for the late reply I did this, howveer my animtions dont play anymore? Why is this
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    That's a very open-ended question. :) Are you still triggering the animation? If you are, did you change the model? Is the rig perhaps set to Generic instead of Humanoid now, or vice versa?