Quaternion problem for character rotation
Hi, guys! I don't understand much of Unity and C#, yet! So, i could use some help from you.
The code that i implemented in my character isn't working as supposed.
I'm tryint to reproduce the same movement style as in the Super Mario game video.
Here's the code:
// Update is called once per frame
void Update() {
CharacterController controller = GetComponent<CharacterController> ();
if (controller.isGrounded) {
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= runningSpeed; //Speed when moving
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed; //1xJump
anim.SetBool ("Jump", true);
} else {
anim.SetBool ("Jump", false);
}
//Rotation to the 8 directions
Vector3 inputDir = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
Vector3 v = inputDir.normalized;
v.x = Mathf.Round (v.x);
v.z = Mathf.Round (v.z);
if (v.sqrMagnitude > 0.1f)
targetDir = v.normalized;
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (targetDir), Time.deltaTime * rotationSpeed);
}
moveDirection.y -= gravity * Time.deltaTime; // Effect of gravity
controller.Move (moveDirection * Time.deltaTime);
}
Here's the issue:
When i type left, my character faces left, like in the Super Mario video posted here, but while facing that new direction, my character moves towards the camera. Same happens when pressing right. And when pressing down, he faces the camera, but instead of moving towards her, he moves in the opposite direction.
Also, he doesn't move in the diagonals, while pressing Up and Right/Left simultaneously.
Console keeps saying: "Look rotation viewing vector is zero"
I hope what i just wrote seems clear to you! Thanks, in advance, for the help given!
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
This seems bit odd to me; usually, the Z component of a vector is "into/out of" the screen, and the Y components is for vertical.
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"),0);
This should change the "in/out" movement into an "up/down" movement.
In Unity, the vertical is defined as the Z axis. The alteration on code that you suggested will make the character's position dependent on the input, that means, when i press up key, the character will move in the Y axis, the same axis used for the jump.
Input.GetAxis ("Vertical");
Does not mean the physical axis, it means 'axis'; as in controller input in terms of a scalar value.
Answer by Namey5 · Apr 24, 2017 at 10:21 AM
moveDirection = transform.TransformDirection (moveDirection);
Remove this line. Because you are rotating the character, transforming the move direction into local space also changes the direction it's moving.
@Namey5 I just removed that line, but without it the character doesn't move. Should i remove the lines where moveDirection is mentioned too?
There's no reason why the character would stop moving, are you certain you removed the correct line and nothing else? All this line does is convert it from a world space direction to a local space direction, i.e. a direction relating to the direction in which the object itself is facing. The whole Update void should be the following;
void Update() {
CharacterController controller = GetComponent<CharacterController> ();
if (controller.isGrounded) {
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection *= runningSpeed; //Speed when moving
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed; //1xJump
anim.SetBool ("Jump", true);
} else {
anim.SetBool ("Jump", false);
}
//Rotation to the 8 directions
Vector3 inputDir = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
Vector3 v = inputDir.normalized;
v.x = $$anonymous$$athf.Round (v.x);
v.z = $$anonymous$$athf.Round (v.z);
if (v.sqr$$anonymous$$agnitude > 0.1f)
targetDir = v.normalized;
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (targetDir), Time.deltaTime * rotationSpeed);
}
moveDirection.y -= gravity * Time.deltaTime; // Effect of gravity
controller.$$anonymous$$ove (moveDirection * Time.deltaTime);
}
Your answer
