- Home /
shooting mechanism similar to sniper games and time crisis
hi! im a complete newbie in unity and for a project i am creating a shooting game where in the camera is in a fixed position and monsters walk pass it, more like a shooting gallery.
so my game so far used an empty object to act as launcher. i also changed my mouse into a crosshair.
my problem now is how to make the launcher be placed at the x and y axis of the mouse on the screen to the world x and y. z would be the launching point so when i shoot it goes to the z axis.
so to make the problem specific. i dont know how to incorporate the mouse position to the x and y position of the launcher.
i tried using the screentoworldpoint and failed badly.
hope someone can help a total noob like me here :D
here is my code of the launcher so far
#pragma strict
static var canThrow: boolean = true;
var throwSound : AudioClip;
public var coconutObject: Rigidbody[];
var throwForce: float;
public var shapes: GameObject[];
var togglebullet : int= 3;
function Start () {
}
function Update () {
//toggle
if(Input.GetButtonUp("Fire2")){
togglebullet++;
BatteryCollect.charge++;
}
if(togglebullet >=3){
togglebullet=0;
}
if(Input.GetButtonUp("Fire1")){
//random fire
// var random : int = Random.Range(0,3);
audio.PlayOneShot(throwSound);
var newCoconut: Rigidbody = Instantiate (coconutObject[togglebullet], transform.position, transform.rotation);
newCoconut.name="coconut";
newCoconut.rigidbody.velocity = transform.TransformDirection(Vector3(0,0, throwForce));
Physics.IgnoreCollision(transform.root.collider,newCoconut.collider,true);
}
}
@script RequireComponent(AudioSource)
Answer by robertbu · Jan 11, 2013 at 08:33 PM
One way is to use use the Plane class (not the plane game object) to create a plane in the scene where you want to place your cross hairs. At each Update() do something like:
Ray ray = camera.ScreenPointToRay (Input.mousePosition);
float dist;
plane.Raycast(ray, out dist);
Vector3 v3Pos = ray.GetPoint (dist);
goCrossHairs.transform.position = v3Pos;