- Home /
How to constantly rotate an object to 0f by X-axis?
Hello Everyone! Newbie question. How to make an object to rotate to 0.0f by X-axis permanently. I mean if it changes only by X-axis then the object automatically and smoothly will rotate to 0f and stops there until another incline. I am doing a tank movement and I did script wherein every time the tank moves forward or backward it inclines and Rotation by X-Axis goes less than 0f. I can't freeze rotations in "Rigidbody/constraints" because it is a child of a blank object with Rigidbody and Box Collider.
I can't use Quaternion.RotateTowards because I don't want my object to change its Y and Z-axis. I will appreciate help with C# language!
public class Tank$$anonymous$$ovement : $$anonymous$$onoBehaviour { Quaternion OriginalRotation; private Rigidbody rb; public Transform tank; private float TurningRate = 10f; private Quaternion rotationGoal = Quaternion.Euler(0f, -178.511f, 0f); private Vector3 RotateToTarget; private void Start() { OriginalRotation = tank.rotation; rb = GetComponent(); } private void Update() { var velositi = transform.InverseTransformDirection(rb.velocity); if (Input.GetKey(KeyCode.W)) { rb.AddForce(gameObject.transform.forward 290f, Force$$anonymous$$ode.Force); if (velositi.z < -10) tank.Rotate(1f, 0f, 0f); } else if (Input.GetKey(KeyCode.S)) { rb.AddForce(gameObject.transform.forward -290f, Force$$anonymous$$ode.Force); if (velositi.z > 10) tank.Rotate(1f, 0f, 0f); }
Debug.Log(velositi.z);
if (velositi.z > 1)
{
tank.Rotate(-0.5f, 0f, 0f);
}
else if (velositi.z < -1)
{
tank.Rotate(-0.5f, 0f, 0f);
}
if (velositi.magnitude < 1)
{
tank.rotation = Quaternion.RotateTowards(transform.rotation, rotationGoal, TurningRate);
}
else if (velositi.magnitude > -1)
{
tank.rotation = Quaternion.RotateTowards(transform.rotation, rotationGoal, TurningRate);
}
float horizontal$$anonymous$$ = Input.GetAxis("Horizontal");
if (horizontal$$anonymous$$ > 0)
gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 100f);
if (horizontal$$anonymous$$ < 0)
gameObject.transform.Rotate(Vector3.down * Time.deltaTime * 100f);
}
}
private Rigidbody rb;
public Transform tank;
private float TurningRate = 10f;
private Quaternion rotationGoal = Quaternion.Euler(0f, -178.511f, 0f);
private Vector3 RotateToTarget;
private void Start()
{
OriginalRotation = tank.rotation;
rb = GetComponent<Rigidbody>();
}
private void Update()
{
var velositi = transform.InverseTransformDirection(rb.velocity);
if (Input.GetKey(KeyCode.W))
{
rb.AddForce(gameObject.transform.forward * 290f, Force$$anonymous$$ode.Force);
if (velositi.z < -10)
tank.Rotate(1f, 0f, 0f);
}
else if (Input.GetKey(KeyCode.S))
{
rb.AddForce(gameObject.transform.forward * -290f, Force$$anonymous$$ode.Force);
if (velositi.z > 10)
tank.Rotate(1f, 0f, 0f);
}
Debug.Log(velositi.z);
if (velositi.z > 1)
{
tank.Rotate(-0.5f, 0f, 0f);
}
else if (velositi.z < -1)
{
tank.Rotate(-0.5f, 0f, 0f);
}
if (velositi.magnitude < 1)
{
tank.rotation = Quaternion.RotateTowards(transform.rotation, rotationGoal, TurningRate);
}
else if (velositi.magnitude > -1)
{
tank.rotation = Quaternion.RotateTowards(transform.rotation, rotationGoal, TurningRate);
}
float horizontal$$anonymous$$ = Input.GetAxis("Horizontal");
if (horizontal$$anonymous$$ > 0)
gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 100f);
if (horizontal$$anonymous$$ < 0)
gameObject.transform.Rotate(Vector3.down * Time.deltaTime * 100f);
}
}
Answer by tuinal · Jun 25, 2020 at 04:01 AM
If you're using physics it's best not to mess around with transforms. Either you go all-in on a physics based controller, or not. If you change transforms, rigidbodies are prone to catapulting around if they move inside a collider. This can be avoided with onerous coding but it's rarely worth the effort.
For a physics-based tank, use e.g. a capsule collider for each track (you can attach multiple colliders to a single gameobject - if you just use a box it will be prone to getting stuck on uneven terrain), and AddTorque or AddForce to it's rigidbody to move it rather than changing the transform. If you need snappier, less realistic movement, set rigidbody.velocity and rigidbody.moveRotation in FixedUpdate rather than transform properties.
Answer by N-8-D-e-v · Jun 24, 2020 at 07:30 PM
Rotate your object by changing EulerAngles like so
transform.rotation = Quaternion.Euler(0, whatever number, whatever number);
Didn't work quite well, now it is not rotating at all, where should I put it? (I have sent the code)
it should actually look like this transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y, transform.eulerAngles.z);