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
  4. Dismiss Notice

Rigidbody based movement

Discussion in 'Scripting' started by Mandalore_the_Creator, Apr 22, 2021.

  1. Mandalore_the_Creator

    Mandalore_the_Creator

    Joined:
    Feb 5, 2021
    Posts:
    10
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovementAltered : MonoBehaviour
    6. {
    7.     public Transform cam;
    8.  
    9.     public float turnSmoothTime = 0.1f;
    10.     float turnSmoothVelocity;
    11.  
    12.     Rigidbody m_Rigidbody;
    13.     public float m_Speed = 5f;
    14.  
    15.     public Animator anim;
    16.     void Start()
    17.     {
    18.         m_Rigidbody = GetComponent<Rigidbody>();
    19.     }
    20.  
    21.     void FixedUpdate()
    22.     {
    23.  
    24.         Vector3 m_Input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
    25.         if (m_Input.magnitude >= 0.1f)
    26.         {
    27.             float targetAngle = Mathf.Atan2(m_Input.x, m_Input.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    28.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    29.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    30.  
    31.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    32.             m_Rigidbody.MovePosition(transform.position + moveDir.normalized * Time.deltaTime * m_Speed);
    33.  
    34.         }
    35.     }
    36. }
    Hello, I'm new in unity. when I made this rigidbody based movement script, I have problem with latency. After I release the key, my character still moving for a quiet bit. but there is no latency when I used joystick. Beside that, my character always move in constant speed instead of following magnitude parameter. Anyone can help me?

    Thank you
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    You can first try using
    Input.GetAxisRaw()
    in place of
    Input.GetAxis()


    Beyond that to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    If you are running a mobile device you can also see the console output. Google for how.
     
    Joe-Censored likes this.
  3. Mandalore_the_Creator

    Mandalore_the_Creator

    Joined:
    Feb 5, 2021
    Posts:
    10
    Thanks, that helpful