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

[CLOSED] My rigidbody jitter

Discussion in 'Physics' started by pappalardodd, Apr 21, 2020.

  1. pappalardodd

    pappalardodd

    Joined:
    Apr 13, 2020
    Posts:
    96
    I have a rigidbody with WASD movements, which rotates according to the point where it looks at the camera. When I start the scene, however, the rigidbody trembles. I tried to mark it "interpolate" and put the movements in FixUpdate (), but the result does not change.
    Can someone help me?
    This is the code of my rigidbody:

    Code (CSharp):
    1. public class movimento : MonoBehaviour
    2. {
    3.  
    4.    [SerializeField] float walk = 1;
    5.    [SerializeField] float run = 2;
    6.     private Rigidbody rig;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.       rig = GetComponent<Rigidbody>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if (Input.GetKeyDown(KeyCode.LeftShift)) walk = (walk + run);
    18.         if (Input.GetKeyUp(KeyCode.LeftShift)) walk = (walk - run);
    19.      }
    20.  
    21.      void FixedUpdate ()
    22.     {
    23.         float Horizontal = Input.GetAxis("Horizontal");
    24.         float Vertical = Input.GetAxis("Vertical");
    25.         Vector3 movement = new Vector3(Horizontal, 0f, Vertical);
    26.          rig.rotation = Quaternion.Euler(0f, Camera.main.transform.eulerAngles.y, 0f);
    27.          rig.position += rig.rotation * movement * walk * Time.deltaTime;
    28.     }
    29.   }
    30.  
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    The Rigidbody isn't moving, you're just instantly teleporting it to a new pose. Interpolation won't work because there's nothing to interpolate as it's not moving.

    Use Rigidbody.MovePosition and Rigidbody.MoveRotation to do this correctly.
     
  3. pappalardodd

    pappalardodd

    Joined:
    Apr 13, 2020
    Posts:
    96
    Hi, very thanks for the reply. Unfortunately I am studying C# but not I still know the terms of unity well.
    However using MovePosition or MoveRotation, I get these error message :

    error CS1656: Cannot assign to 'MovePosition' because it is a 'method group'
    error CS0019: Operator '*' cannot be applied to operands of type 'method group' and 'Vector3'


    So I can't just replace them, but should I use these by changing my script?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    I can't see your code but to guess, you're not using them like you should. Did you look at the code example on the page?

    They are methods (functions) and not properties you assign to so you call them like this:
    Code (CSharp):
    1. rig.MovePosition(rig.position + transform.right * Time.fixedDeltaTime);
    It "sounds" like you're doing this (which is incorrect):
    Code (CSharp):
    1. rig.MovePosition = rig.position + transform.right * Time.fixedDeltaTime;
     
  5. pappalardodd

    pappalardodd

    Joined:
    Apr 13, 2020
    Posts:
    96
    Thanks for the info. I understood the mistake, but despite this once inserted, the object shakes the same.
    Please, could you show me how you would use them on my script? ... I would like to understand if I am wrong or if this is not the problem.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    I can't fix your issue based upon a snippet of code. What you're doing in general seems very odd.

    This part looks suspect though and stands out:
    It seems odd that you're altering the position by the rotation here.

    Also, you should gather your input in the Update and not FixedUpdate and there's plenty of info about this online and in this forums.

    I would suggest making a simpler method first that just moves with no rotation. Use interpolation for smooth movement though obviously.
     
  7. pappalardodd

    pappalardodd

    Joined:
    Apr 13, 2020
    Posts:
    96
    With your advice, I tried to create an object with MovePosition but I can't make it move in the direction it is rotated. What should I add to implement axes on WASD based on rotation?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class movimento : MonoBehaviour
    5. {
    6.    [SerializeField] float walk = 1;
    7.    [SerializeField] float run = 2;
    8.     Vector3 movement = Vector3.zero;
    9.     private Rigidbody rig;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.       rig = GetComponent<Rigidbody>();
    14.     }
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if (Input.GetKeyDown(KeyCode.LeftShift)) walk = (walk + run);
    19.         if (Input.GetKeyUp(KeyCode.LeftShift)) walk = (walk - run);
    20.         float Horizontal = Input.GetAxis("Horizontal");
    21.         float Vertical = Input.GetAxis("Vertical");
    22.         movement = new Vector3(Horizontal, 0f, Vertical);
    23.     }
    24.      private void FixedUpdate ()
    25.     {
    26.    
    27.         rig.MovePosition(rig.position + movement * walk * Time.fixedDeltaTime);
    28.      
    29.     }
    30.   }
     
  8. MuffinMyst

    MuffinMyst

    Joined:
    Nov 17, 2014
    Posts:
    13
    Based on what I see,
    And based on melvmays suggestion

    Have your tried adding in

    "transform.forward" instead of "transform.right"
    ?

    This is just my take on implementing his suggestion, I'm not sure if it works
     
  9. pappalardodd

    pappalardodd

    Joined:
    Apr 13, 2020
    Posts:
    96
    hello, thanks for the reply. You could tell me, how to use MoveRotation to make the object turn in the direction it walks with WASD (I tried a lot but the rigidbody walks but it doesn't rotate in the direction it moves). I think if in addition to moveposition I apply moverotetion the situation can improve (to rotate it I used a script without quaternions at this moment).