Detect if Game Object has been clicked on in 3Dspace
hey so ive been looking around and struggled to find anything on that fitted with what i needed. Anyway so im looking to click on a gameobject while in game then afterwards i want to use that as my target for my missile script however when i come to run it, its saying its not reference to the instance of the object... have i missed something out or does the way im finding the gameobject from the mouse click not like to be used later on in the script? thanks tones in advance here's my code so far:
using UnityEngine;
using System.Collections;
public class turretplayer : MonoBehaviour {
Transform enemy;
public GameObject Missile;
public float enemyDist;
public GameObject spawn;
bool Space = Input.GetKey(KeyCode.Space);
bool fire;
Rigidbody rb;
void OnMouseDown()
{
enemy = gameObject.transform;
}
void Start() { rb = GetComponent(); }
void Update () {
//calc enemy dist
enemyDist = Vector3.Distance(transform.position, enemy.position);
//if enemy is close by
if (enemyDist <= 100)
{
Quaternion neededRotation = Quaternion.LookRotation(transform.position - enemy.position);
transform.rotation = Quaternion.Slerp(transform.rotation, neededRotation, Time.deltaTime * 0.5f);
if(Space == true)
{
Instantiate(Missile, spawn.transform.position, spawn.transform.rotation);
rb.AddForce(transform.forward * 10F);
}
}
}
}
Answer by allenallenallen · Dec 27, 2015 at 04:59 PM
What you wrote:
Transform enemy;
void OnMouseDown()
{
enemy = gameObject.transform;
}
What's enemy? Since you didn't declare the GameObject via script, you need to drag the "enemy" in the editor, which you didn't do. That's why you have the error. Basically, you need a reference to the "enemy" GameObject.
I'm done talking about the error but your approach is problematic. What were you trying to accomplish by setting the position of the enemy to this current GameObject's position when this GameObject is clicked? If you put that to practice, what would happen is that the player clicks on the GameObject this script is attached to and then the enemies will teleport to this GameObject. Are you sure this is what you want?
Oh no, not what i wanted, now looking back at my code i realize. What i originally wanted was for a large Bullet to be fired out by a turret on my player ship. and i wanted the turret to turn it self to face the enemy in the distance only if it was close to the player by 100 radius. I don't want the bullet to aim at the target i wanted it to simple fire out of the turret in a straight line. One problem i keep co$$anonymous$$g into is how to Instantiate the bullet and make it continue traveling for a maximum 10 seconds then if its traveling for more that that it would simple destroy its self.
Could you help me there in anyway?
Your answer
Follow this Question
Related Questions
explosion problem in space shooter 0 Answers
The bolt on "Shooting shots" won't appear 1 Answer
How do I complete homing missile movement? 0 Answers
Missle/Bullet script. C# 2 Answers