- Home /
unity tank tutorial added turret rotation. Problem both player controls move same tanks turret.
I have done unity's tank tutorial from 2015. Now I have added a turret rotation script to the turret tank grandchild of tank prefab and added code in TankMovement.cs to call public function in the turret script and pass float variable with the amount of movement it should rotate.
My problem, the turret will rotate on the blue tank with both player1 and player2 controls. I want player1 controls to move red tank turret and player2 controls to move blue tank turret.
Tank prefab hierarchy Tank: attached TankMovement.cs TankRenderers TankTurret attached Turret.cs
Below is Turret.cs
using UnityEngine;
public class Turret : MonoBehaviour
{
public void Turn(float rotationAmount)
{
// Adjust the rotation of the turret based on the player's input.
transform.Rotate(0, rotationAmount, 0);
}
}
Below is TankMovement.cs marked all code that pertains to turret with //turret
using UnityEngine;
public class TankMovement : MonoBehaviour
{
public int m_PlayerNumber = 1; //also used for turret player controls ********
public float m_Speed = 12f;
public float m_TurnSpeed = 180f;
private string m_MovementAxisName;
private string m_TurnAxisName;
private Rigidbody m_Rigidbody;
private float m_MovementInputValue;
private float m_TurnInputValue;
private float m_OriginalPitch;
//Turret
public float m_TurretTurnSpeed = 1f; //turret ********
private string m_TurretTurnAxisName; //turret ********
private float m_TurretTurnInputValue; //turret ********
private float m_TurretMovevalue; //turret ********
private Turret turretScript; //reference to turret script ********
private void Awake()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
private void OnEnable ()
{
m_Rigidbody.isKinematic = false;
m_MovementInputValue = 0f;
m_TurnInputValue = 0f;
}
private void OnDisable ()
{
m_Rigidbody.isKinematic = true;
}
private void Start()
{
m_MovementAxisName = "Vertical" + m_PlayerNumber;
m_TurnAxisName = "Horizontal" + m_PlayerNumber;
m_TurretTurnAxisName = "Turret" + m_PlayerNumber; //turret ********
turretScript = GameObject.Find("TankTurret").GetComponent<Turret>(); //turret ********
}
private void Update()
{
// Store the player's input and make sure the audio for the engine is playing.
m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
m_TurnInputValue = Input.GetAxis(m_TurnAxisName);
m_TurretTurnInputValue = Input.GetAxis(m_TurretTurnAxisName); //turret ********
if (m_TurretTurnInputValue != 0)
{
m_TurretMovevalue = (m_TurretTurnInputValue * m_TurretTurnSpeed); //turret ********
turretScript.Turn(m_TurretMovevalue); //turret ********
}
}
private void FixedUpdate() //update physics
{
// Move and turn the tank.
Move();
Turn();
}
private void Move()
{
// Adjust the position of the tank based on the player's input.
Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;
m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
}
private void Turn()
{
// Adjust the rotation of the tank based on the player's input.
float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
}
}
link to tutorial, so you know what project it is incase if needed.
Only the Tank hull it's self is a rigidbody with colider. The turret has no rigidbody or collider and is inside the tank colliders box.
Thanks in advance for your help!
Joe ,
Your answer
Follow this Question
Related Questions
Help for LAN multiplayer in C# ? 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Instantiate already connected players. 0 Answers
Master Server GUI not showing up [Errors finally coming up in console] 1 Answer