The question is answered, right answer was accepted
How to rotate object (z-axis only) slowly? C#
I'm trying to make simple moped and i want it to rotate on z-axis when key A or D is pressed.
When i press D to turn right -> moped rotates 30 degrees right (slowly) When i press A to turn left -> moped rotates 30 degrees left (slowly)
Any ideas how i can do that?
I'm using C#
Answer by King_Kurl · Sep 23, 2016 at 06:43 PM
Here you go: Just attach this script to whatever gameobject you want, assign the gameobject you want to rotate under target and set the rotation speed to whatever you want and you should be good to go. I haven't tested this but I'm pretty sure it should work.
using UnityEngine;
using System.Collections;
public class RotateOnZAxis : MonoBehaviour {
//Target rotation speed
public float rotateSpeed = 0.5f;
//Max angle rotation
public float maxAngle = 30.0f;
//Rotationtarget
public Transform target;
//Origin Angle
public float originAngle = 0.0f;
//Reset position?
public bool resetPosition;
//The Current Angle
public float currentAngle;
//Actual rotation speed
private float rotSpeed;
void Update(){
//Calculate target rotation speed
rotSpeed = rotateSpeed * Time.deltaTime;
//Get the current angle
currentAngle = target.localEulerAngles.z;
//If current value is between 180 and 360 degrees, get it's negative equivalent
//This just makes the math easier
currentAngle = (currentAngle > 180) ? currentAngle - 360 : currentAngle;
//If the current angle is greater than the max angle, set it equal to the max angle
if (currentAngle > maxAngle) {
currentAngle = maxAngle;
}
//Or If the current angle is smaller than the negative max angle equivalent,
//set it equal to the negative max angle equivalen
else if(currentAngle < -maxAngle){
currentAngle = -maxAngle;
}
//If press A, rotate the angle to the left, at the specified speed
if (Input.GetKey (KeyCode.A)){
if (currentAngle < maxAngle) {
target.Rotate (0, 0, rotSpeed);
}
}
//If press D, rotate the angle to the right, at the specified rotation speed
if (Input.GetKey (KeyCode.D)) {
if (currentAngle > -maxAngle) {
target.Rotate (0, 0, -rotSpeed);
}
}
//If either A or D is released, then set reset position to true
if (Input.GetKeyUp (KeyCode.A) || Input.GetKeyUp (KeyCode.D)) {
resetPosition = true;
}
//If reset position is set to true
if (resetPosition == true) {
//And as long as A or D are not pressed
if ((!Input.GetKey (KeyCode.A) && !Input.GetKey (KeyCode.D))) {
//If the current angle is greater than the origin angle then decrease it's value
if (currentAngle > originAngle) {
target.Rotate (0, 0, -rotSpeed);
}
//Or if the current angle is less than origin angle then increase it's value
else if (currentAngle < originAngle) {
target.Rotate (0, 0, rotSpeed);
}
//If the current angle is between 0.1 and -0.1 degrees, then set reset to false
if (currentAngle < 0.1f && currentAngle > -0.1f) {
currentAngle = 0;
resetPosition = false;
}
}
}
}
}
When i press D or A it rotates my moped only 30 degrees just like i wanted. But when i drive with my moped and turn it to left or right z-axis goes over 30 degrees.
How i can limit z-axis rotation?
Edit: I think i find solution but i still need help with it.
Any ideas how i can get z-axis rotation get back to 0 when A or D key isn't pressed?
$$anonymous$$y bad, I just saw your comment. There is a multitude of things that could be causing the issue, can you provide more details as to what your set up looks like, and maybe the code you're using to move the moped?
Sure I can help you with that, I'm in the middle of something at the moment so I'll work on it in a bit, give me an hour or two.
One more thing to keep in $$anonymous$$d, this isn't really a place to just ask for code, it's a place to ask for help with your code, and that is if you couldn't find a solution online. But I'm willing to help because you're a new member.
So, Just keep that in $$anonymous$$d for next time.
Alright here you go, I updated the code above to include the added functionality. So now whenever A or D is released the moped should start moving back to the origin point. If you press A or D while it's resetting it's position it will stop the reset process and move in the desired direction, until A or D is released again.
Glad to help! Let me know if you encounter any other issues or need help with something.