- Home /
CAR ROTATING HELP
I have a car that rotates around a cylinder and goes forward (normal driving, but it's on cylidner). When i press left mouse click the car rotates to the left and when i press right mouse click the car rotates to the right. What i want is when i release the buttons the car should rotate to point forward along the cylinder. (Meaning, that you have to hold left/right buttons to steer, and when released the car should go back to middle)
rotation = Input.GetAxisRaw("Horizontal");
Vector3 yRot = Vector3.up * rotation * rotation_speed * Time.deltaTime;
Quaternion deltaRot = Quaternion.Euler(yRot);
Quaternion targetRot = rgb.rotation * deltaRot;
rgb.MoveRotation(Quaternion.Slerp(rgb.rotation, targetRot, 50f * Time.fixedDeltaTime));
Answer by BBIT-SOLUTIONS · Mar 24, 2020 at 03:38 PM
I would suggest you put your car as child in a container GameObject. Then you only change the localRotation on clicking the mousebuttons.
As soon as you release the key, you can simply set the local rotation to 0 and it should point forward again.
Answer by inspired997 · Mar 24, 2020 at 04:23 PM
I have infinite cylinders spawning forward, and the car goes forward infinite, it's a freeride gameplay, so if i add the child in the cylinder it won't work on the others (cloned ones), could you please write me a code for this so i can try it? Thanks in advance!
Well in this case, you can maybe just save the forward direction as a reference and then move to it back later, Here is a (not tested) example code:
Quaternion lookingForward;
void Start(){
lookingForward = transform.localRotation;
}
void On$$anonymous$$ouseUp(){
transform.localRotation = lookingForward;
}
You probably have to adapt it a bit, but the concept should work.