I have a player object with two children, one child has a mouselook script on it and then the camera is attached to it. The other child has my mesh and what not attached to it. When I am not moving the child without the camera is not rotating but when I start to move I want the child without the camera to face the direction of the child with the camera, but turn to it slowly and the direction of ration is what would be closer to the child with the cameras forward direction.
I am having extreme difficultis implementing this.
Any help or suggestions is greatly appreciated!
Thanks for your time.
Have you tried looking at the thirdPersonController script in the Lerpz tutorial? It sounds like, what you’re trying to do, is something similar to what that script is already doing.
Thanks Teriki Tora the 3rd personcontroller helped. I have another question related to character rotation.
I am trying to turn my characters neck bone to face a certain way.
I am using this:
rotationX = script2.rotationX;
xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.left);
transform.rotation = xQuaternion;
The problem is that the character neck bone has a starting rotation of 0,180,180 and when I start up the game it looses that rotation and is in a funky position.
I also tried
transform.LookAt(Vector3(lookattarget.position.x, lookattarget.transform.position.y, lookattarget.position.z));
with no dice.
I cant figure out how to maintain the 0,180,180 ontop of the rotation that I put, I keep getting that quaternians cant be used with vector3s and im finding it difficult. Any help is greatly appreciated!
Found what I was looking for
transform.localRotation.eulerAngles = Vector3(rotationX, 180, 180);
I tried long and hard with the third person controller in the tutorial to get what I needed out of it but I just cant seem to get what I need to work.
Right now I have:
moveDirection = Vector3(inputX * inputModifyFactor, 0, inputY * inputModifyFactor);
//moveDirection = lookattarget.TransformDirection(moveDirection) * runSpeed;
moveDirection = Vector3.RotateTowards(moveDirection, lookattarget.TransformDirection(moveDirection), 3 * Mathf.Deg2Rad * Time.deltaTime, 1000);
The commented out like is what I had before to instantly rotate the character forward, the line below is what I am trying to rotate it to the forward direction slowly but with no luck.
Any suggestions?
EDIT:
The lookatarget is a gameobject that I use my mouselook on so that I can rotate around my character witht the camera without effecting the direction of the character itself, then when I press W or the forward key or any other movement key for that madder it will make the character look in the gameobjects direction. What I want is for him to slowly look toward the game object direction instead of instantly.
Edit:
I was fiddling around and tried something else, I just tried to rotate something from forward vector to a forward vector of another object and still I have trouble
transform.localRotation.eulerAngles = Vector3.Slerp(transform.forward, templookat.transform.forward, 50.0 * Time.deltaTime);
That code doesnt even work and I dont understand why it wouldnt.
You can rotate from one vector to another with Vector3.Slerp:-
moveDirection = Vector3.Slerp(moveDirection, targetDirection, Time.deltaTime);
Hmm I am having trouble, what is wrong with this:
if (faceforward)
transform.rotation.eulerAngles = Vector3.Slerp(transform.forward, targeta.transform.forward, Time.deltaTime);
and the targeta has this script attached to it:
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
transform.rotation.eulerAngles = Vector3(0, rotationX, 0);
For some reason it doesnt make him face forward over time, it doesnt make him faceforward at all, it just makes him face the direction of his parent.
Ok I tried this:
transform.rotation.eulerAngles = Vector3.Slerp(Vector3(0,0,0), Vector3(0,script2.rotationX,0), Time.time);
where script2. is the mouselook script of the other object.
It kinda works but doesnt rotate it over time, it just instantly rotates it. Then when I try this:
transform.rotation.eulerAngles = Vector3.Slerp(Vector3(0,0,0), Vector3(0,script2.rotationX,0), 5 * Time.deltaTime);
It doesnt rotate it over time it just stutters, it doesnt even face him in the right direction.
Im so confused.
EDIT:
I found Quaternion.slerp and it showed a little more information, I tried this:
transform.rotation.eulerAngles = Vector3.Slerp(Vector3(0,0,0), Vector3(0,script2.rotationX,0), Time.time * 0.1);
and it seems to rotate towards it but only once, always in the same direction and somewhat buggy.
EDIT:
GOT IT TO WORK!
With some heavy searching and experimenting this is what I found:
var angle = Quaternion.Angle(transform.rotation, targeta.rotation);
transform.rotation = Quaternion.Lerp(transform.rotation, targeta.rotation, Time.deltaTime * 100.0 / angle);
I was wondering is there a way to add this:
var angle = Quaternion.Angle(transform.rotation, targeta.rotation);
transform.rotation = Quaternion.Lerp(transform.rotation, targeta.rotation, Time.deltaTime * 100.0 / angle);
With this
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = xQuaternion;
So that it will rotate towards the targeta rotation but it will add on the rotation from the input.
This way I can still use the input to turn him in any direction I please while he is turning to face forward / targeta.rotation.
EDIT:
One problem with just this:
var angle = Quaternion.Angle(transform.rotation, targeta.rotation);
transform.rotation = Quaternion.Lerp(transform.rotation, targeta.rotation, Time.deltaTime * 100.0 / angle);
is that if your start turning you character while the lerp is in progress it can lag behind, it can also lag behind if you move your mouse really fast taht is why I need to add the mouse rotation to it.