Hello!
I have the simplest Turret code which doesn't work. It fires projectiles in Z direction (?!) When player is at the positive x positin of the turret the turret shoots upwards in Z, otherwise downwards. It is always facing the player however.
What is wrong?
I attached this code to a simple cube. And the target is a moving player.
using UnityEngine;
using System.Collections;
public class TurretScript : MonoBehaviour {
public GameObject projectile;
public Transform target;
void Start(){
RepeatShooting();
}
void Update(){
transform.rotation = Quaternion.LookRotation (target.position - transform.position);
}
// Shoot and repeat
void RepeatShooting () {
InvokeRepeating ("LaunchProjectile", 2, 0.3f);
}
public void LaunchProjectile(){
//Vector3 projectileStart = new Vector3(
Instantiate (projectile, transform.position, transform.rotation);
}
}
↧