Quaternion snaps back to original position
I'm trying to make an 8-directional movement for my character and I've somewhat succeeded. My problem is that when I press forward button, my character is looking in the direction in which the camera is pointing(as intended), BUT whenever I release the button, it just snaps back to the original position.
Here's the code
private Vector3 inputDir = Vector3.zero;
private float lockRotation = 0.0f;
[SerializeField]
private CharacterController controller;
[SerializeField]
private GameObject gameCam;
[SerializeField]
private Transform transCam;
void Start()
{
transCam = GameObject.FindGameObjectWithTag ("MainCamera").transform;
gameCam = GameObject.FindGameObjectWithTag ("MainCamera");
controller = GetComponent<CharacterController>();
}
void Update()
{
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)
inputDir = v.normalized;
inputDir = Camera.main.transform.rotation * inputDir;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(inputDir), Time.time*5f);
}
void LateUpdate()
{
controller.transform.rotation = Quaternion.Euler(lockRotation, transform.rotation.eulerAngles.y, lockRotation);
}
}
Answer by oStaiko · Aug 24, 2016 at 12:14 AM
Problem
if (v.sqrMagnitude > 0.1f) { Slerp }
Therefore,
if (v.sqrMagnitude < 0.1f) { Not Slerpin } //!!!!
When no keys are pressed, you aint slerpin. Or maybe it's just resetting you to Quaternion.identity somewhere else in your script. You didn't post anything like that so idk.
What I tried:
Update()
{
if ( you wanna slerp) //and you do
{
doSlerp(newDir);
}
}
doSlerp()
{
//Slerp here
}
Now when you're no longer pressing keys, it will continue to slerp. At least in my own test it did. If it doesn't fix yours, I'll come up with something else for ya.
EDIT:
Per request, here's a more elaborate explanation. I'm just gonna show you the scripts I used in an old project that I had used slerp to rotate. I never encountered your problem of snapping back, so idk what's causing it.
void FlyTo (float angle)
{
Quaternion newDir = Quaternion.identity;
newDir.eulerAngles = new Vector3(0,angle,0);
transform.rotation = Quaternion.Slerp (transform.rotation, newDir, Time.time * turnSpeed/3000);
}
That script was great for what I needed. You throw in an angle, and it looks at it! Easy enough.
Here's how I called it, which is an 8 direction system you are using.
Update()
{
float hor = (Input.GetAxisRaw ("Horizontal"));
float ver = (Input.GetAxisRaw ("Vertical"));
if (hor == 0 && ver == 0) {
return;
}
else if (hor == 1 && ver == 0)
{
FlyTo (90);
}
else if (hor == 1 && ver == 1)
{
FlyTo (45);
}
else if (hor == 0 && ver == 1)
{
FlyTo (0);
}
else if (hor == -1 && ver == 1)
{
FlyTo (315);
}
else if (hor == -1 && ver == 0)
{
FlyTo (270);
}
else if (hor == -1 && ver == -1)
{
FlyTo (225);
}
else if (hor == 0 && ver == -1)
{
FlyTo (180);
}
else if (hor == 1 && ver == -1)
{
FlyTo (135);
}
else
{
Debug.Log("Movement Error");
}
}
Now, that's a horrible way to call the function, it's an old project but I know better now. Do what fits your project best.
Could you please elaborate on that a bit more? I'm fairly new to this and it seems like I should've started with something simpler.
Your question is fine, but why did you include Start? What is Controller and why do you need to rotate it
Controller is just used to freeze my y axis? I'm using orbiting camera on my character and it flips my character around without it
Answer by Joyble · Aug 24, 2016 at 02:21 PM
FIXED!
replacing
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(inputDir), Time.time*5f);
with
if ( inputDir != Vector3.zero ) transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(inputDir), 0.5f);
Keeps the direction!
Your answer
Follow this Question
Related Questions
AI targeting player in complete wrong direction 1 Answer
How do I rotate the GameObejcts smoothly with negative degrees 0 Answers
Rotation script isn't working 1 Answer
Getting a 2D object to face the direction of its velocity relative to other objects in the level 0 Answers
How do I smoothly rotate the GameObejcts with negative 90 degrees 0 Answers