Search Unity

Scripting Player Movement

Discussion in 'Community Learning & Teaching' started by DavDevGames, Sep 13, 2019.

  1. DavDevGames

    DavDevGames

    Joined:
    Jul 11, 2017
    Posts:
    9
    Hi. I've been following on the unity learn premium:
    Course
    Advanced Fundamentals: Unity Game Dev Course

    Project
    Unity Gameplay Programming Fundamentals


    And I'm having some troubles smothing the player movment for some reason. I'm using exact same code brought by the instructor.

    The code itself changes the rigidbody.velocity to a value based on player input and camera output.

    - The character moves as expected but the rigidbody.velocity keeps resetting to "0" even when getting constant input by the player.

    - While the jitter is barley noticeable for the movment it affects badly any camera following and the animator that changes animations form idle to running based on the rigidbody.velocity.

    Things that I already tried:
    - Using the same package as the tutorial( same models, game objects, scenes).
    - A constant rigidbody.velocity free from player input for input testing.

    Any insights or directions for study?

    Code (CSharp):
    1. public class Movement : MonoBehaviour
    2. {
    3.     Rigidbody rg;
    4.     public int speed;
    5.     Vector3 movmentVelocity;
    6.     Animator anim;
    7.     private Camera mainCamera;
    8.     Vector3 test;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         rg = GetComponent<Rigidbody>();
    14.         anim = GetComponent<Animator>();
    15.         mainCamera = Camera.main;
    16.  
    17.         test = new Vector3(3, 0, 0);
    18.      
    19.     }
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         //input
    24.         float h;
    25.         float v;
    26.         h = Input.GetAxis("Horizontal");
    27.         v = Input.GetAxis("Vertical");
    28.         Vector3 movmentInput = new Vector3(h, 0f, v);
    29.    
    30.         //camera and rotation logic
    31.         Vector3 cameraForward = mainCamera.transform.forward;
    32.         cameraForward.y = 0;
    33.         Quaternion relativeRotation = Quaternion.FromToRotation(Vector3.forward, cameraForward);
    34.         Vector3 lookTowards = relativeRotation * movmentInput;
    35.         if (movmentInput.sqrMagnitude>0)
    36.         {
    37.             Ray lookRay = new Ray(transform.position, lookTowards);
    38.             transform.LookAt(lookRay.GetPoint(1));
    39.         }
    40.  
    41.         movmentVelocity = transform.forward * speed * movmentInput.sqrMagnitude;
    42.         Animate();
    43.     }
    44.  
    45.     private void FixedUpdate()
    46.     {
    47.         movmentVelocity.y = rg.velocity.y;
    48.         rg.velocity = movmentVelocity;
    49.         //rg.velocity = test;
    50.     }
    51.  
    52.     void Animate()
    53.     {
    54.         anim.SetFloat("Speed", rg.velocity.sqrMagnitude);
    55.     }
    56. }
     
    Last edited: Sep 13, 2019
  2. DavDevGames

    DavDevGames

    Joined:
    Jul 11, 2017
    Posts:
    9
    Coming from a 2d background I tought I would need to manually move the GameObject around the scene but the animation itself promotes movment
    Code (CSharp):
    1. rg.velocity = movmentVelocity;
    .
    This line was actually conflicting with the movment promoted by the animation. After some adjustments I've managed to solve the issue. Please close or delete this topic. Thanks!