Problem with my tank script

Hello again! I have a Body as a parent and a Gun as a child of the Body and a script attached to the Body. Now here is the problem. I have made WASD control for the body and (mouse follow control) for the Gun. When i press Play not only the gun follow the mouse but the Body too. How to set (mouse follow control) to work only for (Gun), which is child of Body.

if(gameObject.name == "Body") {
    var horiz : float = Input.GetAxis("Horizontal") ;
        transform.Rotate(Vector3(0, horiz, 0) * 60 * Time.deltaTime, Space.World) ;
    var vertic : float = Input.GetAxis("Vertical") ;
        transform.Translate(Vector3(0, 0, vertic) * movespeed * Time.deltaTime) ;
}
if(gameObject.name == "Gun") {
    var playerPlane = new Plane(Vector3.up, transform.position);
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hitdist = 0.0;
    if(playerPlane.Raycast (ray, hitdist)) {
    var targetPoint = ray.GetPoint(hitdist);
    var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotatespeed * Time.deltaTime);
    }
}

I hope you understand me! Thank you!

1 Answer

1

your camera needs to be the parent of the gun...then add the body and camera as a child of the character controller (which will have your mouse look script). don't put the script on the camera itself -- only the controller.