- Home /
[C#] Quaternion Rotations with Input.GetAxis problems.
Hello, I am New to Unity however I have been doing Game Maker for a couple of years now and so I know how make codes on an intermediate level. Now, I don't know all the functions that is within Unity or the inner workings of it. But I am having trouble with some code. Now I have managed to make a Movement script for my Player within a 3D space in C# Code and I have been figuring out Quaternion Rotations and looking up and researching it. What I am trying to achieve with this code is after 3D movement that can make the player go in 8 directions as you would have in most games I am sure. I have a void that is for moving and I have one for turning ( So void Move() and void Turn() ) they are both called by the Update function. Now what I want is to go for an old PS1 3D platformer where you don't need to walk in a circle to rotate. All you have to do is press a button and the player snaps to that direction. So in this case the GetAxis. I have those working but when he does snap to the direction that I have pressed he doesn't go in that one direction that I want him too, instead he goes in the same direction no matter whichever way he faces ( Faces forward, turn to face Left, He moves in the direction he was originally facing). So I figured it must be just the model being rotated not the object. So with any luck could somebody maybe give me some ideas or fix up some of my code for me? Again, I am new to the program itself but coding I am pretty good at.
Here is my code: [RequireComponent(typeof(Rigidbody))] [RequireComponent(typeof(Animator))]
public class PlayerMovement : MonoBehaviour {
//Variables.
Animator Anim;
Rigidbody rBody;
// Quaternion targetRotation;
public int Walkspeed = 10;
//public int Runspeed = 10;
//public int Ground = 1;
//public int RotateVEL = 360;
public int JumpHeight = 16;
public float JumpSpd = 10f;
public Quaternion TargetRotation;
void Awake () {
Anim = GetComponent<Animator> ();
rBody = GetComponent<Rigidbody> ();
}
void Start () {
//Turn ();
}
// Update is called once per frame
void Update () {
Move ();
Turn ();
}
void Move () {
// Moving Forward, Backwards and Left and Right.
float Z = Input.GetAxis ("Vertical");
float X = Input.GetAxis ("Horizontal");
transform.Translate (X * Time.deltaTime * Walkspeed, 0, Z * Time.deltaTime * Walkspeed);
Anim.SetFloat ("Forward", Input.GetAxis ("Vertical") );
}
//Rotations.
void Turn() {
//float Z = Input.GetAxis ("Vertical");
//float X = Input.GetAxis ("Horizontal");
if (Input.GetAxis ("Horizontal") > 0)
transform.rotation = Quaternion.LookRotation (Vector3.right);
// transform.LookAt (Vector3.right * Time.deltaTime * Walkspeed );
// transform.Rotate (Vector3.up * Time.deltaTime * RotateVEL);
//transform.Rotate (Vector3.right * Time.deltaTime * Walkspeed);
else if (Input.GetAxis ("Horizontal") < 0)
transform.rotation = Quaternion.LookRotation (Vector3.left);
if (Input.GetAxis ("Vertical") < 0)
transform.rotation = Quaternion.LookRotation (Vector3.forward);
else if (Input.GetAxis ("Vertical") > 0)
transform.rotation = Quaternion.LookRotation (Vector3.back);
}
}
I have tried a lot of things. And its taken me about 5 days to get this far with working it out by myself so any help would be nice and appreciated. Again, I just basically want to use the GetAxis function to move in 8 directions (for my 360 Controller mainly) and the character or the player facing the direction and him being able to move in that direction when held down.
Thank you. Sorry for the long post but I sometimes have a hard time explaining stuff ^^'
Answer by regred002 · Jul 08, 2016 at 06:45 PM
Maybe try the Transform.LookAt command with a set of transforms that are dependent on the player? like
if (Input.GetKey (KeyCode.u)) {
transform.LookAt(new Vector3(transform.position.x+5,transform.position.y,transform.position.z));
}
if (Input.GetKey (KeyCode.J)) {
transform.LookAt(new Vector3(transform.position.x-5,transform.position.y,transform.position.z));
}
That should work (given a couple other sets of these), But it will be quite jittery if you attach a camera to it.
I don't use axes at all but I would image you could just use the code you made earlier instead of my key inputs.