- Home /
How to calculate xSpeed and ySpeed according to float value of rotation?
Hey guys, We are working on a bullet hell game and we spent days trying to figure out how to change xSpeed and ySpeed according to bullets rotation. We finally figured that we can do it like this:
moveDirection = new Vector3(xSpeed, ySpeed, 0f);
moveDirection = transform.TransformDirection(moveDirection);
//Previous line adjusts our xSpeed and ySpeed according to the rotation of this object.
But the problem is that we need rotation of the object for something else. So we are trying to figure out a formula how to change xSpeed and ySpeed according to the rotation without using transform component. Here is one way we tried doing it but it failed short:
moveDirection.x = xSpeed * Mathf.Cos(currentRotation);
moveDirection.y = ySpeed * Mathf.Sin(currentRotation);
//currentRotation is our float that should determine rotation of this object
or
float radians = (Mathf.PI / 180f) * (currentRotation - 90f);
moveDirection.x = xSpeed * Mathf.Cos(radians);
moveDirection.y = ySpeed * Mathf.Sin(radians);
This one would work well if we just had one bullet speed. But since we have both xSpeed and ySpeed that both need to be affected by rotation, this code is not working out.
Do you guys know any way how we can calculate both xSpeed and ySpeed according to float value of rotation.
Thanks!
Answer by bobisgod234 · Jun 15, 2017 at 07:09 AM
if I understand correctly, you want to use your X and Y speed variables as the forward and right speed of the object, but programmatically rotate the forward and right of the object (the angle).
Try:
Vector3 transformedSpeed = Quaternion.AngleAxis(currentRotation, Vector3.up) * new Vector3(xSpeed, 0f, ySpeed);
moveDirection.x = transformedSpeed.x;
moveDirection.y = transformedSpeed.z;
Thanks @bobisgod234 You have no idea how much you helped us with this!
Your answer
Follow this Question
Related Questions
2D Simulated Sword Swing - Sword Floating Away 0 Answers
Rotation relativ animation paramter 0 Answers
Object leaning with 2D movement 2 Answers
How can I straighten my character's movement and actions? 0 Answers
Flight Movement with Banking 2 Answers