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:
//In the Start() function:
movement = rb.transform.rotation * rb.transform.forward;
//for FixedUpdate:
private void FixedUpdate()
{
if (isMovingForward == true)
{
rb.MovePosition(rb.position + maxSpeed * Time.fixedDeltaTime * -movement);
}
else if (isMovingBackwards == true)
{
rb.MovePosition(rb.position + maxSpeed * Time.fixedDeltaTime * movement);
}
//Credit to the Unity Forum guys for helping me figure out the move position issues
if (isRotatingRight == true)
{
rb.transform.Rotate(Vector3.up * (rotateSpeed * Time.fixedDeltaTime));
}
else if (isRotatingLeft == true)
{
rb.transform.Rotate(Vector3.down * (rotateSpeed * Time.fixedDeltaTime));
}
}
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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
[SerializeField] private float maxSpeed = 10;
[SerializeField] private float maxAngSpeed = 20;
public bool isMovingForward = false;
public bool isMovingBackwards = false;
public bool isRotatingRight = false;
public bool isRotatingLeft = false;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
//for FixedUpdate:
private void FixedUpdate()
{
// tidied up the if statements, you don't need to compare a bool, you can use it directly
Quaternion deltaRotation;
Vector3 AngularVel = Vector3.zero;
// generate a sign dependent angular velocity about y (up)
if (isRotatingRight)
{
AngularVel = transform.up * maxAngSpeed;
}
else if (isRotatingLeft)
{
AngularVel = transform.up * -maxAngSpeed;
}
// convert your angular velocity to a quaternion angle change
deltaRotation = Quaternion.Euler(AngularVel * Time.fixedDeltaTime);
// 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)
rb.MoveRotation(deltaRotation * rb.rotation);
// do the rotation first, then you can just move forward
if (isMovingForward)
{
rb.MovePosition(rb.position + maxSpeed * Time.fixedDeltaTime * transform.forward);
}
else if (isMovingBackwards)
{
rb.MovePosition(rb.position - maxSpeed * Time.fixedDeltaTime * transform.forward);
}
}
}
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.
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.
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
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.