I want to add barrel rolling to RaceCraft. I just want it so that when you press the Roll button your craft rotates once around its z axis, and still lets you steer while it happens. I tried a few things that resulted in failure. Any thoughts? Thanks!
… make your craft a child of a GO and add your controls to the GO. Then you can rotate the craft independently of the GO.
I was actually asking how I would get it to rotate 360 degrees and then stop with one press of the button, and make sure that it will stop at exactly 0 degrees.
How about a coroutine that you start when the button is pressed? All the routine does is modify the rotation. Are you using physics to control your vehicles or is through direct manipulation of the transforms?
It’s Physics. I can get the thing to barrel roll (transform.Rotate(Vector3.forward*10)). The trick is getting it to stop when I want it to.
If your craft is physics controlled it should be relatively simple to add in torques that rotate it while turning and generally keep it level, but then also flip it around when the button it pressed. (this is assuming that you are using mostly world space forces. The turning will break while spinning if you are using local space for the forces.
If you want the rolling to be transform controlled, make the racecraft body a child of the actual controlled rigidbody and then rotate it with code. Something like:
var initialJumpSpeed = 0.20;
var rotateAcceleration = 3.00;
var rotateDeceleration = 2.00;
var rotateSpeed = 500;
var minAllowance = 1.00;
private var rotation = 360.00;
function Update ()
{
if(rotation < 180)
{
rotateAmount = Mathf.Clamp01(initialJumpSpeed + ((rotation / 180) * rotateAcceleration) );
}
else
{
rotateAmount = Mathf.Clamp01((((360 - rotation) / 180) * rotateDeceleration) );
}
if(Input.GetButtonDown("Jump") (transform.localEulerAngles.z < minAllowance || transform.localEulerAngles.z > 360 - minAllowance)) rotation = 0;
curRotate = rotateAmount * rotateSpeed * Time.deltaTime;
rotation += curRotate;
transform.Rotate(Vector3.forward * curRotate);
}