- Home /
A smooth single-axis rotaion (fps camera)
ok, im still relatively a beginner at c# im slowly working on a game (so far so good).
but right now my issue is i have to floating hands that orbit the players side (by the way this is a first person game), but what im trying to currently do is make it so that when looking to the sides i want the hands to gradually realign themselves, or make them stay in place unless the amount turned is significant enough. but i have no idea where to start.
so far all i have is void LateUpdate()
{
transform.position = target.position + new Vector3(0, 0, 0);
transform.rotation = target.transform.rotation; //this is a placeholder
}
thx in advance.
Answer by robertbu · Feb 27, 2013 at 07:23 AM
Below is a script for your functionality. To make it work, the location of the axis of rotation for the hands must match the axis of the camera. You can do this in one of two ways. You can either author the hands with the center at the correct location, or you can use an empty game object. Place the empty game object at the same position as the camera (*don't make it a child). Then position the hands so they look the way you want with respect to the camera view. Finally make the hands a child of the empty game object. If you use the second method, the script goes on the empty game object, not on the hands:
public class HandRotator : MonoBehaviour {
private Vector3 v3Offset;
private GameObject goFollow;
private float speed = 0.5f;
void Start () {
goFollow = Camera.main.gameObject;
v3Offset = transform.position - goFollow.transform.position;
}
void Update () {
transform.position = goFollow.transform.position + v3Offset;
transform.rotation = Quaternion.Slerp (transform.rotation, goFollow.transform.rotation, speed * Time.deltaTime);
}
}
hey thanks for directing me here, but i dont understand what you mean when you say that the rotation must match a camera?
If you place the hands (or light) in the exact same place as the camera (i.e. their positions as viewed in the inspector are identical) do the hands (or light) look the way you want them to look? In the case of the light it will more be about how the light plays in the scene. If the answer is yes, then the camera and the hands/light will share the same origin and you can attach the script to the hands/light. If the answer is no, then you have to do the "trick" I outline with the empty game object. The object that is pivoting must placed at the same position as the camera in the editor. If the hands/light need to be some place else, you can make the a child of the object that is at the correct position. Actually in the case of a light since it doesn't have a physical object near the camera, you might be able to get away with some offset.
ok well for those who dont understand my delima i made a diagram
pretty much when you view goes to the left for example,you catch a glimpse of your hand dependent on how fast you turn. this is probably the best i can explain it. and im going to try robertbu's answers later today when i get the chance.
As long as you setup the rotation point of the hands to match the origin of your object, the above code should work for allowing the use to "catch a glimpse of your hand."
Your answer
Follow this Question
Related Questions
How to rotate the axis of a 3D GameObject 1 Answer
How to state synchronize the rotation of one axis? 1 Answer
FPS Run Animation Problem with Rotation 0 Answers
different axis for different objects? 1 Answer
Rotating wrong 0 Answers