- Home /
The question is answered, right answer was accepted
Rotating character to a direction (2.5d)
Hello,
First, I'd like to apologize because I know this question has been asked plenty of times. Unfortunately, even after reading through the dozens of topics, I am still unable to comprehend how to achieve what I want.
Basically, I am working on a 2.5D game, where the player may only move left or right. I would like the character to face the direction the character controller is moving in. For now, I've managed to achieve what I want somewhat using the following script on my character controller:
float moveHorizontal = Input.GetAxis ("Horizontal");
moveDirection = new Vector3 (moveHorizontal * speed, moveDirection.y, 0.0f);
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
controller.Move (moveDirection * Time.deltaTime);
if (moveHorizontal > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
if (moveHorizontal < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
However, instead of automatically setting the rotation to a new Vector 3 (0, 180, 0) when moving left, I'd like the player to rotate to that direction . I've tried doing transform.rotation = Quaternion.LookRotation(moveDirection), but it makes my character move weirdly and look all over the place. I just need him to rotate on the Y axis. Alternatively, if someone could point me to a basic tutorial on quaternions (I already checked the Unity ones, I'm struggling to understand this concept) that would be of great help. Thanks.
Answer by FabDynamic · Oct 24, 2017 at 01:35 AM
I think I see what you mean and that sounds cool! Instead of a Mario type game you want it to actually animate the rotation smoothly from left to right. That will end up cool!
What you want to do is called 'lerp-ing'. I think the word evolved from 'interpolate' or but it involves just the kind of smooth transition you are interested in.
Starting with the example here you'll want to change the Transforms from and to into specific rotations. Something like this:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Quaternion from = Quaternion.euler (0f, 0f, 0f);
public Quaternion to = Quaternion.euler (0f, 180f, 0f);
public float speed = 0.1F;
void Update() {
transform.rotation = Quaternion.Lerp(from, to, Time.time * speed);
}
}
Here are some more details: http://answers.unity3d.com/questions/643141/how-can-i-lerp-an-objects-rotation.html
Oh and wrt Quaternions: I don't know of any tutorials, but I also struggle to understand this concept however Im starting to realize that we don't need to understand it. This has worked for me so far: Just think of them as something not to mess with directly. In order to adjust rotations use the built in functions and anytime you need to specify the angles specifically use Euler angles exclusively. I hope that helps.
Let me know if for any reason that doesn't work and I'll see if I can prototype it.
UPDATE: I prototyped it and made a video to show it working: http://youtu.be/stXNSrXyF1k?hd=1
Wow, thanks so much for all the effort you put into this! I'll check this out as soon as I can and provide an update on my situation. Thanks a lot for your help!
You're welcome! I'm looking forward to the update and seeing how well it works for you and your purposes.
Follow this Question
Related Questions
Character Controller - unknown rotation, flying 1 Answer
Character Controller slides sideways when it hits objects are angles different from 90 degrees 1 Answer
How do I add camera rotation while moving a player sideways? 2 Answers
Rotate CharacterController collider? 0 Answers
CharacterController.move doesn't seem to move me horizontally 0 Answers