Search Unity

Question how to make rigidbody.MovePosition relative to rotation?

Discussion in 'Physics' started by mackayaj, Feb 5, 2021.

  1. mackayaj

    mackayaj

    Joined:
    Feb 3, 2021
    Posts:
    7
    Hello everyone;

    So I need a little help with regards to MovePosition and transform.Rotate. I've been having issues with coding my script that is supposed to make a gameobject move in a fixed speed and in the direction of the rotation. I've gotten the movement part down; I can get my gameobject to move continuously on command at a fixed speed, which I got help from previously on the forums. Now, I just need help with the rotation part.

    Here's the code so far:

    Code (CSharp):
    1. //In the Start() function:
    2. movement = rb.transform.rotation * rb.transform.forward;
    3.  
    4. //for FixedUpdate:
    5.  
    6. private void FixedUpdate()
    7.     {
    8.         if (isMovingForward == true)
    9.         {
    10.             rb.MovePosition(rb.position + maxSpeed * Time.fixedDeltaTime * -movement);
    11.         }
    12.         else if (isMovingBackwards == true)
    13.         {
    14.             rb.MovePosition(rb.position + maxSpeed * Time.fixedDeltaTime * movement);
    15.         }
    16.         //Credit to the Unity Forum guys for helping me figure out the move position issues
    17.  
    18.         if (isRotatingRight == true)
    19.         {
    20.             rb.transform.Rotate(Vector3.up * (rotateSpeed * Time.fixedDeltaTime));
    21.         }
    22.         else if (isRotatingLeft == true)
    23.         {
    24.             rb.transform.Rotate(Vector3.down * (rotateSpeed * Time.fixedDeltaTime));
    25.         }
    26.  
    27.  
    28.  
    29.     }
    When I run the code like this, I can get the gameobject to rotate, however when I tell it to move forward, it just moves forward in a straight line regardless of what direction it's facing. Ideally, it would move in whatever direction it was facing, so if it was both moving and rotating it would make a sort of circle motion.

    Can anyone help? Thanks!
     
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    In the same style as your previous script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     [SerializeField] private float maxSpeed = 10;
    8.     [SerializeField] private float maxAngSpeed = 20;
    9.  
    10.     public bool isMovingForward = false;
    11.     public bool isMovingBackwards = false;
    12.     public bool isRotatingRight = false;
    13.     public bool isRotatingLeft = false;
    14.  
    15.     private Rigidbody rb;
    16.  
    17.  
    18.     void Start()
    19.     {
    20.         rb = GetComponent<Rigidbody>();
    21.     }
    22.  
    23.     //for FixedUpdate:
    24.  
    25.     private void FixedUpdate()
    26.     {
    27.         // tidied up the if statements, you don't need to compare a bool, you can use it directly
    28.         Quaternion deltaRotation;
    29.         Vector3 AngularVel = Vector3.zero;
    30.  
    31.         // generate a sign dependent angular velocity about y (up)
    32.         if (isRotatingRight)
    33.         {
    34.             AngularVel = transform.up * maxAngSpeed;
    35.         }
    36.         else if (isRotatingLeft)
    37.         {
    38.             AngularVel = transform.up * -maxAngSpeed;
    39.         }
    40.  
    41.         // convert your angular velocity to a quaternion angle change
    42.         deltaRotation = Quaternion.Euler(AngularVel * Time.fixedDeltaTime);
    43.  
    44.         // mulitply the current rotation quaternion by the quaternion change (delta) and move to that new rotation (for quaternions, multiplying can be thought of as like adding Euler  angles)
    45.         rb.MoveRotation(deltaRotation * rb.rotation);
    46.  
    47.         // do the rotation first, then you can just move forward
    48.         if (isMovingForward)
    49.         {
    50.             rb.MovePosition(rb.position + maxSpeed * Time.fixedDeltaTime * transform.forward);
    51.         }
    52.         else if (isMovingBackwards)
    53.         {
    54.             rb.MovePosition(rb.position - maxSpeed * Time.fixedDeltaTime * transform.forward);
    55.         }
    56.      
    57.     }
    58. }
    You should not set the transform rotation directly for rigid bodies. For kinematic bodies use MoveRotation

    It might be worth checking some tutorials on rigids to see how they can be moved and used - kinematic and non-kinematic are different and work in different ways.
     
  3. mackayaj

    mackayaj

    Joined:
    Feb 3, 2021
    Posts:
    7
    Thank you for another response, but unfortunately this doesn't seem to be working. It simply does the same thing that it was doing previously. If I wasn't as clear as I should've been, I wanted the gameobject to move in the direction/rotation it was facing; so if it is both isRotatingRight and isMovingForward are both true, for example, the gameobject should hopefully move in a right-handed circle.

    Sorry; I'm very new to Unity and I don't fully understand concepts like rigidbodies yet.
     
  4. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    Here it is, try importing it into a clean 2020.2.1f1 session. Works for me.
     

    Attached Files:

  5. mackayaj

    mackayaj

    Joined:
    Feb 3, 2021
    Posts:
    7
    PROBLEM: I can see what you did, but now I can't go back to my own scene. Again, I don't really know what I'm doing, and it seems like opening this just deleted everything that I made in my original script. Again, I'm really new to Unity so I don't know what I'm doing xD
     
  6. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    I did say "try importing it into a clean 2020.2.1f1 session". I don't believe it should have overwritten anything without warning you (yellow exclamation on import). If you had assets with the same name they will have been overwritten e.g. Movement.cs and the default SampleScene. Unless you are using a version control system or taking copies they won't be retrievable. I'm very sorry that has happened, if you would like some direct assistance recoding your work, direct message me and I'll find some time to help you next week.
     
    MelvMay likes this.
  7. mackayaj

    mackayaj

    Joined:
    Feb 3, 2021
    Posts:
    7
    No, I don't need anything like that. I can easily just recreate everything with no real difficulty. Thank you so much for the help anyways!
     
    AlTheSlacker likes this.
  8. mackayaj

    mackayaj

    Joined:
    Feb 3, 2021
    Posts:
    7
    ANOTHER UPDATE: I got it working! Finally!
    Thank you for the help!
     
    AlTheSlacker likes this.