- Home /
Set Object X-Rotation to Its Vertical Speed (with a 360º Limit)
Hello! My name is Juan and there's something I need to put in my game. I want my GameObject to stay parallel/(whatever the name is for 90º intersection, I forget.) to the parabola of movement when jumping. Kinda like this; ][1]
My character is that cat-like thing and the parabola is, well, the parabola of height/distance (neither are accurate).
Thanks in advance to whoever helps!
I had to re-type this because I thought I closed it and then I posted it and turns out it was open in another tab XD
there is a few options to setting an objects rotation/position You could use a Vector3.Lerp or directly set the eulerAngles, if your object has rigidbody, there is also AddRelativeTorque and Force, if you'd like examples for any of these let me know and ill write some.
vector3.lerp sounds fancy so let's go with that :)
Answer by skylem · Jan 21, 2015 at 04:55 AM
this is an example of a Lerp, one for the position another for rotation via quaternion. the end position and rotation will be copied from the transform assigned to target. hope this helps you sorry for the delay.
public Transform target;
void Start () {
}
// Moving the object this script is on from Position A, to Position B, the last value is the frames it does this over.
// lerp can also be used with quaternion values.
void Update () {
transform.position = Vector3.Lerp (transform.position, target.position, 0.01f);
transform.rotation = Quaternion.Lerp (transform.rotation, target.rotation, 0.01f);
}
Edit -- Additional information on how to Limit Rotations. the below code uses an objects Z axis rotation to apply force if we breach our minimum or maximum.
public float curZ;
public float minZ = 330f;
public float maxZ = 30f;
void Update () {
curZ = rigidbody.transform.localEulerAngles.z;
if(curZ > maxZ) {
if(transform.localEulerAngles.z > 180 && transform.localEulerAngles.z < minZ) {
rigidbody.AddRelativeTorque(Vector3.forward * rigidbody.mass);
}
if(transform.localEulerAngles.z < 180) {
rigidbody.AddRelativeTorque(Vector3.back * rigidbody.mass);
}
}
}
// creating a float for the distance between object A and B in this case the object the scripts on from the object we set as ground.
float distance;
Transform ground;
distance = Vector3.Distance (transform, ground.transform);
i'm kind of confused. can I just stick this into my script and it'll work? I don't know what the ending position will be, so there's that.
Can you show me manually setting up the eulerAngles, so I can see if it makes more sense to me? :\ But thank you, it's just that you're too smart for me ;D
The way it works is that the character will be running, and the player can make him jump. when he jumps I want it to be like it's "pouncing" kind of so it needs to be facing the way I showed in the diagram, but the thing is that it's meant to bounce off of other stuff mid-air and so the height/distance parabola will reset, and I think the most productive way to make the character behave like that is by setting the x rotation relative to the vertical speed. If you have another, more useful way to do this, that'd be great.
oh, whoops. i meant to put it so that the limit is 360º. will change the title right now!
hey yea no problem the Euler angles are the rotation values of the Transform on your Gameobject, basically the above code stops an object from rotating past 30 and -30 330 respresents -30 a simpler use example could include
// if the objects rotation exceeds or is equal to 10 degrees
if(rigidbody.transform.eulerAngles.x >= 10) {
// then we lock it to 10 degrees
// done by creating a new Rotation value from a Vector3
// if you fill the last field 0,0,0 the rotation would be x=0,y=0,z=0 in this case its 10 for x because thats what we're setting
rigidbody.transform.eulerAngles = new Vector3(10,0,0);
}
This code is just an example of a eulervalue i don't recommend using it for anything.
in answer to your other question i believe setting the rotations relative to your vertical speed is a good way to do this, u should also factor in your characters distance from the objects he is going to land on, this way the rotation of your creature will be relative to his height i added a distance check to my answer.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Multiple Cars not working 1 Answer
[C#] Rotate Turret, Only On X and Z Axis 2 Answers
Unfreezing rotation causes the position and rotation go crazy 0 Answers
How to rotate mouse addresses? 0 Answers