Rotate Rigidbody on Y Axis based on Velocity on X and Z axis
I have a Rigidbody (cube) that I am moving on the X and Z axis by adding force to it.
I'd like to have its "face" rotate toward the velocity (on the Y axis) so that it is facing the direction it is moving.
I am sure this is simple enough, but after googling for over an hour and trying out some different solutions I can't seem to get it to work.
I know the code to rotate is simple enough:
rb.rotation = Quaternion.Euler(0, heading, 0);
But calculating "heading" is my issue. I can do it with a bunch of conditionals and make heading equal the some hard set rotations ... but there has to be a better way.
Thanks in advance.
Answer by Hyubusa · Jul 23, 2016 at 05:35 AM
Basing it off of Velocity proved to be a bit ... awkward, just based it off of general direction instead. But, it gets the goal accomplished:
using UnityEngine;
using System.Collections;
public class Physics_PlayerController : MonoBehaviour
{
public float speed;
private Rigidbody rb;
void Start()
{
speed = 15;
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.WakeUp();
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
float heading = Mathf.Atan2(moveHorizontal, moveVertical) * Mathf.Rad2Deg;
if (moveHorizontal != 0 || moveVertical != 0)
{
rb.rotation = Quaternion.Euler(0, heading, 0);
}
}
}
Answer by TheShadyColombian · Jul 22, 2016 at 06:20 PM
Calculate the average (i think) of the x and y velocities of the rigidbody that is moving ( heading = (yourRigidBody.velocity.x + yourRigidBody.velocity.y)/2
)
That, I believe, is how you calculate the heading
variable. Let me know what happens. (Word of advice, as I have learnt in the past, this won't end well if you player has a constant velocity and you don't compensate for it in the median value calculation. Ie: an infinite runner)
Thank You.
This got me going in the right direction at least ... some excessive spinning on start up and some ... jittering on movement stopping. But, maybe I can figure it out?
This is what I used to calculate heading:
float heading = $$anonymous$$athf.Atan2(rb.velocity.x, rb.velocity.z) * $$anonymous$$athf.Rad2Deg;
The simple code so far ... if anyone wants to help out further it would be appeciated.
public class PlayerController : $$anonymous$$onoBehaviour
{
public float speed;
private Rigidbody rb;
void Start()
{
speed = 15;
rb = GetComponent<Rigidbody>();
Debug.Log("we started");
}
void Update()
{
}
void FixedUpdate()
{
rb.WakeUp();
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
float heading = $$anonymous$$athf.Atan2(rb.velocity.x, rb.velocity.z) * $$anonymous$$athf.Rad2Deg;
rb.rotation = Quaternion.Euler(0, heading, 0);
}
}
Your answer
Follow this Question
Related Questions
Rotate Rigidbody towards target rotation using AddTorque() 0 Answers
I want to make a script that causes the directional light to stop at 180 degrees. 0 Answers
Can I make an object move at the same speed as touch using Rigidbody.MovePosition()? 0 Answers
Freeze rotation of just the box collider component 0 Answers