- Home /
move based on camera position
I made a script to move around and it works decent, but the turns aren't smooth, they're instant and I can't figure out how to improve my code to make it smooth. I tried a couple of things with quaternions, but they're completely new to me and sofar I'm not getting close.
This is the code I'm trying to smooth out:
if(vertical > _delay && horizontal > _delay)
{
transform.forward = _cameraController.transform.forward;
transform.Rotate(0, 45, 0);
} else if(vertical > _delay && horizontal < -_delay)
{
transform.forward = _cameraController.transform.forward;
transform.Rotate(0, 315, 0);
} else if (vertical < -_delay && horizontal < -_delay)
{
transform.forward = _cameraController.transform.forward;
transform.Rotate(0, 225, 0);
}
else if (vertical < -_delay && horizontal > _delay)
{
transform.forward = _cameraController.transform.forward;
transform.Rotate(0, 135, 0);
} else
{
if (vertical > _delay)
{
transform.forward = _cameraController.transform.forward;
}
if (horizontal > _delay)
{
transform.forward = _cameraController.transform.forward;
transform.Rotate(0, 90, 0);
}
if (vertical < -_delay)
{
transform.forward = _cameraController.transform.forward;
transform.Rotate(0, 180, 0);
}
if (horizontal < -_delay)
{
transform.forward = _cameraController.transform.forward;
transform.Rotate(0, 270, 0);
}
}
Basicly, my character moves away from camera with W, towards camera with S, to the left of camera with A and to the right of camera with D. I managed to smooth it out some by adding an input delay, but it should be possible to improve this and I'd like to know how:)
Edit: This is still what I want to achieve, but I don't think the problem is my understanding of quaternions after looking into them some more. The thing I still can't figure out though, is how to properly relate this to say, the camera, or the cameracontroller. Here I'm using fixed numbers to turn a set amount after making sure my character faces forward, I have no clue how to change those fixed numbers into a variable related to my camera, which is the input I'd need for the quaternions if I'm not mistaken.