I have two questions, they are both for a character in the 2.5D side scroller game I am working on. Think basic WASD character movement, press “d” he goes forward while playing run animation, press “a” he is supposed to turn, and go the other way…
How can I clamp the characters Y rotation to -90 and 90? The reason being…
I am trying to set up a simple way to rotate the character’s direction when you change direction. So if the character is originally facing 90 when moving forward in the level, if you press the other direction/input button, or “a”, I need it to rotate to -90 and play that animation. Right now the character will not rotate to -90. ( I want to clamp because when the level starts the character rotation changes to 90.00001 for some reason, and its messing up my code… Check out // LEFT in the code
Input on either of these would be great, my code is below. The clamp attempt below is NOT working, im getting an error because its not being using correctly. (The game is an X - Y sidescroller, however my character imported with the axis screwed up, so I am using its Z for the movement in this script)
Javascript
// Player Script : Movement
// Inspector Variables
var playerSpeed : float = 5.0;
function FixedUpdate () {
// Clamp Rotation to 90
transform.rotation.y = Vector3(Mathf.Clamp(-90, 90)); // **Clamp attempt NOT working**
// Character Movement
// RIGHT
if (Input.GetButton ("Move Right"))
{
transform.Translate (0, 0, playerSpeed * Time.deltaTime);
}
if (rigidbody.velocity.z > 0)
{
animation.CrossFade("CRun");
}
// LEFT
if (Input.GetButton ("Move Left"))
{
transform.Translate (0, 0, -playerSpeed * Time.deltaTime);
}
if ( Input.GetButton ("Move Left") transform.rotation == 90)
{
transform.Rotate ( 0, -90, 0);
}
if (rigidbody.velocity.z < 0)
{
animation.CrossFade("CRun");
}
// IDLE
if (rigidbody.velocity.z == 0)
{
animation.CrossFade("CIdle");
}
// JUMP
if (Input.GetButtonDown ("Jump2"))
{
rigidbody.velocity = Vector3 (0, 12, 0); // Jumps in air
rigidbody.velocity.y -= 4;
}
}
Just tried it, but it didnt work. I think the problem is that the character’s rotation is never actually == 90, because for some reason when I hit Play, the rotation is 90.0001, regardless that it is set to 90 in the scene view. So I think that is why it won’t actually execute the rotation. Im trying to figure out how to clamp the Y rotation to 90 and -90 so that the rotation actually does == 90, and hopefully the rotate will work. Any ideas regarding the clamp or any other fix would be greatly appreciated.
Having a problem with the character rotating back and forth after a while. He eventually falls off the platform, maybe because of the weird 90.0001 rotation issue? Should I still try and clamp this?