- Home /
Making bullets go to the crosshair
I am using raycasts to make a gun but I want the bullets going to the centre of the screen. I saw a fragment of a code to do this. I am new to raycasting, and help would be appreciated.
Comment
Best Answer
Answer by creative72 · May 22, 2011 at 05:37 PM
var crosshair : GameObject; // game object that goes where the ray collides. make this an empty game object.
var bullet : GameObject;// the projectile being fired.
var pnt : GameObject; // point where bullet is instantiated from. place this object where you want bullet to fire from.
function Update () {
var ray = Camera.main.ScreenPointToRay (Vector3(Screen.width/2,Screen.height/2,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 10000)) {
Debug.DrawLine (ray.origin, hit.point);
crosshair.transform.position = hit.point;
pnt.transform.LookAt(hit.point);
}
Instantiate(arrow,pnt.transform.position,pnt.transform.rotation);
Unless I am mistaken, this just creates the CrossHair where the hit was, not making the bullet go inlineto the middle.
Your answer
![](https://koobas.hobune.stream/wayback/20220612082212im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making raycasts go to the centre of the screen 1 Answer
How to increase score on hit 0 Answers
Raycasts hitting the crosshair 1 Answer
Text Alignment 2 Answers
Hitmark display on hit 1 Answer