0
votes

Character is the parent object and has camera object as its child object (FPS type).

Camera object has a script which 'throws' raycast to detect objects in front of it.

Camera object has a child object 'Crosshair', which has a script 'ShowCrosshair' attatched to it.

Character <- Camera <- Crosshair.

Expected behaviour should've been: I move the mouse up and down, camera rotates around y-axis and along with it the crosshair object moves smoothly remaining at the center of the screen.

But, whenever I move the mouse up/down, crosshair moves faster than the camera can rotate and goes out of sight.

Here's my code for Camera Rotate:

 //vertical is float and is initially zero and transform is for the current gameObject i.e Camera
 vertical -= Input.GetAxis("Mouse Y");
 transform.localRotation = Quaternion.Euler(new Vector3(vertical, 0.0f, 0.0f));

This script is attached to Camera object.

It is desired that Crosshair move with camera that is why it's parented by camera object.

2

2 Answers

0
votes

I think your unwanted behaviour happends because whenever you move, rotate or scale the parent Transform all the childs are affected, therefore when you rotate the camera, the crosshair moves with it and then you rotate the crosshair causing it to rotate a second time and going out of sight.

0
votes

It look like you are rotating around the X-axis instead of the Y (vertical) in this line:

transform.localRotation = Quaternion.Euler(new Vector3(vertical, 0.0f, 0.0f));

Also you should probably multiply the affect of the input by the delta time or it will move much too quickly.

vertical -= Input.GetAxis("Mouse Y") * Time.deltaTime;

But I don't think any of that is your problem. You commented that the script is attached to Camera. Is it also attached to Crosshair? Because if so then you will be getting double rotation. If not, take a look at what scripts might be changing the rotation of Crosshair. If you are just moving the camera, you shouldn't need anything rotating Crosshair at all (because it will move with its parent).