The question is answered, right answer was accepted
[Solved] Unusual Forward Movement When Rotating
I have ship that moves forward in the direction the transform is facing. When I rotate this ship, however, I get unexpected movement.
Here's a gif showing the behavior that I am seeing: https://media.giphy.com/media/1g2C1sk8eHEko24QIq/giphy.gif
What I am expecting is the ship to move the way that the camera is: in a complete circle. But, for some reason, it moves in one direction, then kind of halts, then keeps moving in that one direction.
Here's the code I am using:
public class TestMovement : MonoBehaviour
{
[SerializeField] Transform childObject;
[SerializeField] float rotationAmount = 40f;
[SerializeField] float smoothing = 2.0f;
[SerializeField] float movementSpeed = 20f;
private void Update()
{
// Get Player Input
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical") * -1;
// Calculate Movement
CalculateMovement(horizontal, vertical);
// Calculate Rotation
CalculateRotation(horizontal, vertical);
// Move forward in direction of transform's forward
transform.position += transform.forward * Time.deltaTime * movementSpeed;
}
void CalculateMovement(float horizontal, float vertical)
{
// Set a Vector 3 with the direction we wish to move in
Vector3 direction = new Vector3(horizontal, vertical, 0);
// Move the ship in the desired direction
transform.position += direction * movementSpeed * Time.deltaTime;
// Rotate the transform for free-range movement
transform.Rotate(0f, horizontal * 1.5f, 0f);
}
private void CalculateRotation(float horizontal, float vertical)
{
Quaternion target = Quaternion.Euler(vertical * -rotationAmount, horizontal * rotationAmount, horizontal * -50f);
childObject.localRotation = Quaternion.Slerp(childObject.localRotation, target, Time.deltaTime * smoothing);
}
}
If you can help me figure out what I'm doing wrong here, I would greatly appreciate it. And just in case it is needed, here's an image showing the inspector.
I have some issues trying to understand your code, pretty sure the problem is that you are updating the the transform twice can you try removing this line inside calculatemovement? transform.position += direction movementSpeed Time.deltaTime;
xxmariofer is correct, you are definitely counting each movement twice. It would also help to know which kind of movement you want to achieve. Are you going for a starfox style scifi shooter or something more akin to dogfighting in ships?
The problem wasn't updating the transform twice. The first transform update moved the object on it's transform's forward. The other moved it up and down, left and right, so there is 0 conflict there.
The problem was I was moving it left and right when it should just rotate ins$$anonymous$$d. In the line: Vector3 direction = new Vector3(horizontal, vertical, 0);, it takes horizontal and vertical input from the player. The second time was transform.Rotate(0f, horizontal * 1.5f, 0f);, which rotates the object about the Y axis.
The first horizontal isn't needed from the direction, since the transform.Rotate was already rotating the ship.
Also, if I may ask, what part of the code is hard to understand? I thought I did a good job commenting it, but if not please let me know.
Your calculatemovement method was a bit confusing, not for you since you code it, but for someone who trys to understand why were you rotating the spaceship in that method when you have a method already only for rotating the spaceship, also you are updating the movement twice, will make the player move twice as fast in certain situations. but your comments were much nicer than $$anonymous$$es thats for sure. :)
Ah okay. Sorry about that. In my game, I have two types of movement: On Rails and Free Range. The final controller handles both types of movement in one class, but at the time I made this post, I had created a temporary secondary class using code copied and pasted over from the main class to get the desired animations and stuff in place. This secondary class was made to create the free range movement since the On Rails was working perfectly. After fixing that one bug with the free range movement, I combined the two classes together and simplified it further and put more comments in.
I try to make sure I put in good comments so that when I look at the class a year or two down the line I know what is going on without stressing my brain.
Here's what the on rails movement looks like: https://media.giphy.com/media/9PrDqGc5QoD$$anonymous$$b94e25/giphy.gif
And here's the barrel roll and banking.
Answer by Tetra · Jan 20, 2019 at 10:51 PM
Nevermind! I figured it out. It was the horizontal movement under CalculateMovement. The following changes fixed everything!
// Set a Vector 3 with the direction we wish to move in
Vector3 direction = new Vector3(0f, vertical, 0f);
// Move the ship in the desired direction
transform.position += direction * movementSpeed * Time.deltaTime;
// Rotate the transform for free-range movement
transform.Rotate(0f, horizontal * 1.5f, 0f);