- Home /
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!
Answer by Velketor · Mar 15, 2011 at 03:45 AM
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.
Your answer
Follow this Question
Related Questions
How to steer vehicle with mortorTorque or different wheel speed in each wheel (for Tanks movement) 0 Answers
Simple Tank Controller 2 Answers
MMD How to export model and animations to Unity as 3rd person controller? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Tank Controller/Script? 1 Answer