How to set a max rotation angle
Hey guys. I'm fairly new to scripting so I apollogise if this turns out to be really basic. I'm building a car controller rig. I have got a basic move script attached to the prefab as a whole and it works fine but I am wanting to set up some underlying scripts to control the wheels so they turn with the car to make it feel more realistic. I have the rotating down good but I cannot figure out how to set a maximum rotation so the wheels don't just keep rotating around into the wheel well. After spending days trawling the net for a sokution to my problem but cannot find a relevant answer. I have tried learning as much as I can about quaternions and euler angles but so far nothing I jave learned aboit them has helped. If anyone could give me some examples would be great and if you could explain why and how that way would work would be even better.
Here is a brief example of my rotating wheel code:
If (Input.GetKey (KeyCode.D)) { transform.Rotate (Vector3.up turnspeed Time.deltaTime) ; }
And this just basically repeats for each direction witha GetKeyUp function to return the wheels back to being adjacent with the car ounce it is done turning
Answer by TheSorm · Apr 27, 2016 at 10:38 AM
Sonthofen like that shult work:
If (Input.GetKey (KeyCode.D)) {
If (Transformation.eulerangel() < yourangel){
transform.Rotate (Vector3.up turnspeed Time.deltaTime) ; }
}
(Don't know if eulerangel is the right function name)
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D)){
if (transform.eulerAngles() < maxangle ); {
transform.Rotate (Vector3.up * turnspeed * Time.deltaTime);
}
}
I have tried this way as well as several other different ways of phrasing it and just keep getting this error message ; error CS1955: The member `UnityEngine.Transform.eulerAngles' cannot be used as method or delegate
Soory i ride that on my phone small mistake its just eulerAngles without "()"
If (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D)) {
If (transform.eulerAngles < yourangel){
transform.Rotate (Vector3.up turnspeed Time.deltaTime) ; }
}
Algood. I thought the () seemed odd. Am still Getting Compiler errors though This Is my full Wheel Script. I think I am not setting the eulerAngles float right? I don't really know how to do this properly.
using UnityEngine;
using System.Collections;
public class WheelsTurnScript : $$anonymous$$onoBehaviour {
public float turnspeed = 50f;
public float ReturnNormal = 100f;
public float maxrotation = 30f; //To Set $$anonymous$$ax Rotation
void Update () {
if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D)){
if (transform.eulerAngles < maxrotation){
//Compiler Error = Operator '<' Cannot Be Applied to Operands Of Type 'UnityEngine.Vector3' And 'UnityEngine.Transform'
transform.Rotate (Vector3.up * turnspeed * Time.deltaTime);
}
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A)) {
if (transform.eulerAngles < maxrotation){
//Compiler Error = Operator '<' Cannot Be Applied to Operands Of Type 'UnityEngine.Vector3' And 'UnityEngine.Transform'
transform.Rotate (Vector3.up * -turnspeed * Time.deltaTime);
}
}
//This Part To Return Wheels To Normal After Turning
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.A)) {
transform.Rotate (Vector3.up * ReturnNormal * Time.deltaTime);
}
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.D)) {
transform.Rotate (Vector3.up * -ReturnNormal * Time.deltaTime);
}
}
}
transform.EulerAngel is a Vector3 you have to say with rotation you need like "transform.eulerAngel.x"
Is Almost working now Cheers. All Compiler errors are now gone, the only problem is when the object rotate's it doesn't stop completely and max rotation angle I have set, it just pauses for about a second and then continues to rotate until it gets to the next rotation point and then it does the same. for example it will go 45° and then pause, then it will go another 45° and then pause and so on and so fourth. What do you think Could be causing this and how should I go about fixing/changing it. Here is the kinda "Core" part of this script :
void Update ()
{
if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D)){
if (transform.eulerAngles.y > maxrotation) {
transform.Rotate (Vector3.up * turnspeed * Time.deltaTime);
}
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A)) {
if (transform.eulerAngles.y > maxrotation){
transform.Rotate (Vector3.up * -turnspeed * Time.deltaTime);
}
}
Thanks for your input so far has been really helpful
Ehe do you use if(transform.eulerAngles.y > maxrotation){ And not if(transform.eulerAngles.y < maxrotation){
And to fix the problem with the steps just look in your inspector and look how the rotation change and what can cause this problem
Your answer
Follow this Question
Related Questions
Child Object Rotation interpolation problem, 0 Answers
How can I set rotation properly? 0 Answers
Swapping Right handed Quaternions to left handed from streaming data. 1 Answer
Why raycast2d not work? 0 Answers
Relative rotation on a custom axis? 0 Answers