TrueWinter
TrueWinter

Reputation: 59

Unity Getting A Gun To Follow A Camera?

I am using this to get my gun moving with the camera but it doesn't rotate with the camera for some reason.

//Updating Position
transform.position = cameraToFollow.transform.position + (Quaternion.Euler(0, targetYRotation, 0) * new Vector3(holdSide, holdHeight, 1));

targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraToFollow.transform.rotation.x, ref targetXRotationV, rotateSpeed);
targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraToFollow.transform.rotation.y, ref targetYRotationV, rotateSpeed);

transform.rotation = Quaternion.Euler (targetXRotation, targetYRotation, 0);

transform.parent = cameraToFollow.transform;

Upvotes: 0

Views: 2328

Answers (1)

Hobbyist
Hobbyist

Reputation: 16202

This isn't going to be an answer which provides a solution to your code, but more or less a design hint. I've created quite a few first-person games, with my current project being a large-scale FPS called desert storm. Instead of writing a script to attach the gun to the camera, simply set the gun as a child of the camera in the inspector.

If you're changing between first and third person like I do, you'd simply change the parent of the transform whenever you go to 3rd person.

Upvotes: 1

Related Questions