- Home /
Raycasting to the middle of the screen?
I've found out how to make a gun shoot and thats great, but the code won't shoot directly in the middle of the screen (AKA crosshair). Any help? EDIT: I've noticed that the effect is played straight from the gun (right of the player). It's not taking in the rotation, so it won't shoot where i want too. So anyway to force it to take rotation/shoot to the middle of the screen?
Gun Script:
var shotSound: AudioClip; // drag a shot sound here, if any
var bloodPrefab: GameObject; // drag the blood prefab here, if any
var sparksPrefab: GameObject; // drag the sparks prefab here, if any
function Update(){
if (Input.GetButtonDown("Fire1")){
Shoot();
}
}
function Shoot(){
if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound
var hit: RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit)){
// prepare rotation to instantiate blood or sparks
var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
if (hit.transform.tag == "Enemy"){ // if enemy hit...
if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot);
hit.transform.SendMessage("ApplyDamage", 5,
SendMessageOptions.DontRequireReceiver); // and consume its health
} else { // otherwise emit sparks at the hit point
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
}
}
What is this script attached to? What is the position of this game object? Does the position of this game object ever change?
When you say the code won't "shoot" directly in the middle of the screen, have you seen any "patterns"? Where does the code shoot at? How do you "know" it isn't shooting in the middle of the screen?
Always assume when posting questions that the reader doesn't know what you are describing so be as clear as possible, ideally hosting pictures or diagrams. The more time you spend creating a well written question, the more likely you are to get a well considered and knowledgable answer.
I assume you probably have this script attached to your gun which is positioned off to either side of your player. What you should do ins$$anonymous$$d is attach the script to an empty game object (or even the main camera if it FPS) and place it so the raycast is pointed straight out in front of the player.
Answer by Bunny83 · Jun 08, 2012 at 03:00 PM
It's easier to use Camera.ViewportPointToRay. The example on this page exactly shows what you want ;)
// Unitycript
var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
if (Physics.Raycast (ray))
{
// ...
}
Hmm. Truly so, truly so. $$anonymous$$ay as well provide more accurate results (Screen.width/height do calculate the entire screen resolution, whether windowed or not). +1 for better answer. :)
Answer by asafsitner · Jun 08, 2012 at 01:58 PM
I believe using `Camera.ScreenPointToRay` will help.
Physics.Raycast(Camera.main.ScreenPointToRay(Screen.width / 2, Screen.height / 2, 0))
Should cast a ray directly to the middle of the screen.
Your answer
Follow this Question
Related Questions
Making a shotgun? 1 Answer
FPS PISTOL 1 Answer
How do I make a gun project a particle? 1 Answer
How do I make a gun? 1 Answer