gun movement

sorry for asking this question again i am trying to find solution for pat to week .I have a game object name gun as parent and gun nozzle and camera as child to gun.I am using first person mouse rotation script.I want my gun nozzle only visible not whole gun .when i rotate move the mouse inside the game screen the gun nozzle should rotate and nozzle front should always point to the mouse as mouse is the target sign for the gun .when ever target
sign move inside the screen with mouse nozzle should point to the mouse point.I should able to rotate the gun nozzle in 360 degree also with mouse.I tried many script not getting the effect i wanted.Can you please help to solve this issue

1 Answer

1

Why Don’t you Do it like This?

public class NewBehaviourScript : MonoBehaviour {
	
	public GUIText txt;
	public GameObject gameobject ;
	public Camera cam;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		//Ray ray = camera.ViewportPointToRay(
		txt.text = string.Format("Mouse X {0} , Mouse Y= {1}",Input.mousePosition.x/Screen.width,Input.mousePosition.y/Screen.height);
		RaycastHit hit;
		Ray ray = cam.ViewportPointToRay (new Vector3(Input.mousePosition.x/Screen.width,Input.mousePosition.y/Screen.height,0f));
		if (Physics.Raycast (ray,out hit)){
				gameobject.transform.LookAt(hit.point);
		}
	}
}