- Home /
Fire weapon from turret to mouse position (3D Isometric shooter)
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;
}
}
}
}
}`
Answer by robertbu · Mar 07, 2013 at 07:02 AM
I made a few changes to your script and posted it below. I think most of your problems were in the last line...the way you were shooting the projectile:
using UnityEngine;
using System.Collections;
public class Turret2 : MonoBehaviour {
public Transform cannon;
public GameObject bullet;
private GameObject spawnPt;
void Start () {
spawnPt = GameObject.Find ("oneSpawn");
Vector3 myTransform = cannon.transform.forward;
}
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
Vector3 myTransform = cannon.transform.forward;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit Hit;
if (Physics.Raycast (ray, out Hit, 1000)) {
if (Hit.collider.name == "Plane") {
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.AddRelativeForce(Vector3.forward * 2000);
}
}
}
}
}
It spawns now, but it goes up in velocity only, and the laser rigidbody just falls back down.
I tested the code before I posted it, and it worked fine on my machine. I'm not sure what a 'laser' rigidbody is since it is not in the code above. Take this code and put it in a new scene. Create a cube for the 'cannon', and put a spawn point in front of it (positive Z side). $$anonymous$$ake sure the bullet you use is a real prefab, not a scene item. Try it and let me know how it works.
Your answer
Follow this Question
Related Questions
2d Position to 3d Position 2 Answers
Game Object flies out of screen when dragged 1 Answer
Move player with mouse help 0 Answers
Instantiating Object On Map 1 Answer
Having an object point towards mouse position in 3d world 1 Answer