- Home /
Barrel rotation Y, not working well with rigidbody.
Basically I have a barrel with a rigidbody and a sphere collider attached to it. What I'd like to achieve is, when the barrel moves forward, it rotates around the Z axis as a barrel would in real life. That's easily achieved by wether a forward wheter a backward force.
BUT the problem comes when I'd like to rotate it around the Y axis, so I can turn with it. I was trying to chang it's rotation by having a rotY variable, which I increase or decrease depending on which direction to turn, then I convert that into a Vector3 which then I convert to a Quaternion with EularAngles and lastly I change the transform's rotation. It rotates to left and right but then the rotation to the Z axis doesn't work..
I was also trying with rigidbody.MoveRotation() but couldn't really figure it out how should it work for me perfectly. Anyone knows the solution ?
Here's my code:
public class Movement : MonoBehaviour {
private Rigidbody rb;
public float forward, backward, rotY;
public Vector3 rotation;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
rotY = 90.0f;
}
// Update is called once per frame
void Update () {
//Moving with keys
if (Input.GetKey(KeyCode.UpArrow)) {
rb.AddForce(Vector3.forward * forward, ForceMode.Acceleration);
} else if (Input.GetKey(KeyCode.DownArrow)) {
rb.AddForce(Vector3.back * backward, ForceMode.Acceleration);
}
if (Input.GetKey(KeyCode.LeftArrow)) {
rotY -= 1.0f;
} else if (Input.GetKey(KeyCode.RightArrow)) {
rotY += 1.0f;
}
if (rotY > 360.0f || rotY < -360.0f) {
rotY = 0.0f;
}
rotation = new Vector3(rb.rotation.x, rb.rotation.y + rotY, rb.rotation.z);
//Rotation
rb.rotation = Quaternion.Euler(new Vector3(rb.rotation.x, rotation, rb.rotation.z));
}
}
Answer by toromano · Oct 19, 2015 at 01:46 PM
You could try giving torque in global up axis for steering:
void FixedUpdate()
{
//steering
float turn = Input.GetAxis("Horizontal");
rb.AddTorque(Vector3.up * speed * -turn);
//driving
float throttle = Input.GetAxis("Vertical");
rb.AddTorque(transform.up * speed * throttle);
}
Works lik a charm if I only use the //steering part. Thank you !
Answer by RChrispy · Oct 19, 2015 at 01:49 PM
I think this is 3D? :D
And you rotate the local axis? Of your model? If you use a 3d Model parent this model under an empty ( if you didnt do it) its just better to do so because later changes with the models are safer that way. Another reason is if you use blender it will get problematic.
I would Make an empty with the rotation: 0 0 0 the model with: 90 0 0 (blender cylinder parented at empty)
And rotating the Z axis only: Quaternion.Euler(0f,0f, rotY);
Hope this helps you a little maybe you can describe your setup a little bit more if this doesn't help ;)
I don't see why are you being cynical. $$anonymous$$aybe you just can't express yourself in a good way. Also if I didn't mention any 2d components like RIgidBody2D, and I was using Vector3-s ins$$anonymous$$d of Vector2-s, why wouldn't you assume it's in 3D ?
Dont know what you mean I am not a native english speaker and I just made my points clear. Told you about better practice not only how to fix the problem. But you can just pretend to see insults and ignore it. I dont see any special problems in the text above just help and advice to setup your things differently because later on you get problems.
ps: And yeah I overlooked the rigidbody component and therefore the question for 3d this was no mean question at all, you should be more chill^^.