Hey So I have a script set up for a “Missile Command” type arcade shooter. The perspective is a 3d isometric view so I want the turret to be the origin for the raycast and fire off onto the enemies that appear. I can’t get any ray or instantiate to correctly work. Here is my script:`
using UnityEngine;
using System.Collections;
public class Turret2 : MonoBehaviour
{
public Transform cannon;
public Transform bullet;
private GameObject spawnPt;
// Use this for initialization
void Start ()
{
spawnPt = GameObject.Find ("oneSpawn");
Vector3 myTransform = cannon.transform.forward;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey (KeyCode.Mouse0)) {
Vector3 myTransform = cannon.transform.forward;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit Hit;
if (Physics.Raycast (ray, out Hit, 1000)) {
if (Hit.collider.gameObject.CompareTag ("Sky")) {
Debug.DrawRay (transform.position, myTransform, Color.red);
GameObject newProjectile = Instantiate (bullet, spawnPt.transform.position, Quaternion.identity) as GameObject;
newProjectile.transform.LookAt (Hit.point);
newProjectile.rigidbody.velocity = newProjectile.transform.forward * 10;
}
}
}
}
}`