- Home /
Basic Shooting Game
i have created first person controller and gun is the child of the main camera of first person. I have created prefab game object which i have spawn at different location. Now i want to target this prefab object using my gun.
I have refered this tutorial for my basic start. Now i want to shoot this cube game object with mu first person controller gun. So what should i code to make target.?
Some of the coding i have written but with this it is not hitting these cubes.
Here is my code:
var shotSound: AudioClip; // drag a shot sound here, if any
// the PickupController component of the 'PickupSpawnPoints' GameObject
private var pickupController:PickupController;
function Awake()
{
// retrieve the PickupSpawnPoints gameObject
var pickupSpawnPoints:GameObject = gameObject.Find("PickupSpawnPoints");
// and then retreive the PickupController Component of the above PickupSpawnPoints gameObject
pickupController = pickupSpawnPoints.GetComponent("PickupController");
}
function Update(){
if (Input.GetKey(KeyCode.Space)) {
Shoot();
}
}
function Shoot(){
if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound
var hit: RaycastHit;
var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
if (Physics.Raycast (ray ,hit , 200))
{
//var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
Debug.DrawLine (ray.origin, hit.point);
if (hit.transfer.tag == "Pickup"){ // if enemy hit...
pickupController.Collected(hit.transfer.gameObject);
}
}
}
I am very new in unity and still learning concepts and scripting in unity. so please help me to target my game object. Thanx for your help and support in advance.
Answer by SubatomicHero · Feb 26, 2013 at 04:19 PM
you probably need to add a public variable for your target. Something like this:
var target : GameObject;
// or this depending on your choice
var target : Transform;
then you can drag your target onto that variable in the editor and access it in the script
Good Luck!
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Timing Between each Gun Shot 2 Answers
Object Does not Destroyed While Shooting 1 Answer
Bullet does not move forward 1 Answer
Bullet Script Help 3 Answers