- Home /
How do I ray cast shoot to mouse-crosshair from mesh and not camera?
Here's a video of my very flawed mechanics, I've posted in the forum but I'm gonna ask here about a specific issue that occurs at 2:01 http://youtu.be/3s7YBz-8WXQ
Here is the script attached to camera which is a child object mouseorbiting around a player mesh the spawnPoint of the bullet prefabs is a child of the camera so the bullets shoot out from the camera lol. What i moved the spawn point to the mesh's hand or something it was aiming totally off and offset:
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour {
public AudioClip shootSound;
public float shootForce = 2500;
public GameObject spawnPoint;
public GameObject bulletPF;
public GameObject Character;
private GameObject projectile;
public bool isLaserState = false;
void Update() {
if(isLaserState){
if(Input.GetMouseButton(0)) {
audio.PlayOneShot(shootSound);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);//hit exits now
projectile = Instantiate(
bulletPF,
spawnPoint.transform.position,
spawnPoint.transform.rotation) as GameObject;
Debug.DrawLine(spawnPoint.transform.position, hit.point);
projectile.transform.LookAt(hit.point);
Physics.IgnoreCollision(projectile.collider, Character.collider);
projectile.rigidbody.AddForceAtPosition(ray.direction * shootForce, hit.point);
}
}else if (Input.GetMouseButtonDown(0)) {
audio.PlayOneShot(shootSound);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);//hit exits now
projectile = Instantiate(
bulletPF,
spawnPoint.transform.position,
spawnPoint.transform.rotation) as GameObject;
Debug.DrawLine(spawnPoint.transform.position, hit.point);
projectile.transform.LookAt(hit.point);
Physics.IgnoreCollision(projectile.collider, Character.collider);
projectile.rigidbody.AddForceAtPosition(ray.direction * shootForce, hit.point);
}
}//end of Update
}//end of class
When the Raycasting comes from another object, you have to decide how you are going to handle the triangulation.
Image the hex is the camera with the red line the Ray from the mouse position. As you can see there are an infinite number of possible positions along the red line that the third party (blue triangle) can select as the aim point. So you can 1) pick a distance from the camera to calculate the point, or 2) you can Raycast and find a point and then do the raycast from the third party to that point. Or you can combine the two and use a distance if the raycast fails.
right! so i actually moved the raycast origin to the spawn point, and it works somewhat now. http://youtu.be/C35uRioxTpI
I still lookat(hit) but now i : projectile.rigidbody.velocity = projectile.transform.forward * shootForce;
ins$$anonymous$$d of what's on line 50. but now when i shoot off into nothing. it veers off into the x axis?? ... ohh wait do i have to use ray.direction??
Answer by shahan · Aug 04, 2014 at 07:29 AM
So I actually just got it to work with some help from #unity3D on IRC. credit definitely goes to user Dezoaan on #unity3D
this code fixes it: It has everything to do with the new variable added in called 'aimPoint' which is a vector3 that if hit.point = (0.0,0.0,0.0) then aimPoint = to a point 100 units into the ray. so now you have a point to lookAt no matter what!
Here's a youtube video of it working. notice how spawnPoint is placed in front of the model now. resembling some sort of gun shooter or bullet emitting area. http://youtu.be/Ivtx2oykB1g
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour {
public AudioClip shootSound;
public float shootForce = 2500;
public GameObject spawnPoint;
public GameObject bulletPF;
public GameObject Character;
private GameObject projectile;
public bool isLaserState = false;
int ammo = 20;
void Update() {
if(isLaserState){
if(Input.GetMouseButton(0)) {
audio.PlayOneShot(shootSound);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//straight ray to mouse position
Vector3 aimPoint;//hit point placeholder
if (Physics.Raycast(ray, out hit, 100)){//where the ray hits, so draw from anywhere to THIS = raycast from new THAT
aimPoint = hit.point;
} else { //if ray doesn't hit anything, just make a point 100 units out into ray, to referece
aimPoint = ray.origin + (ray.direction * 100);//aimPoint is some point 100 unitys into ray line
}
projectile = Instantiate( bulletPF, spawnPoint.transform.position, spawnPoint.transform.rotation) as GameObject;
projectile.transform.LookAt(aimPoint); //fixes when hit point was = (0,0,0);
Debug.DrawLine(spawnPoint.transform.position, aimPoint);
Physics.IgnoreCollision(projectile.collider, Character.collider);
projectile.rigidbody.velocity = projectile.transform.forward * 80;//this plus the LookAt aimpoint sends a bullet on the correct ray
ammo = 20;
}
// END OF LASERSTATE SHOOTING
}else if (Input.GetMouseButtonDown(0) && (ammo > 0)) {//normal shooting, since button down
audio.PlayOneShot(shootSound);
ammo--;
RaycastHit hit; //hit exists now
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 aimPoint;
if (Physics.Raycast(ray, out hit, 100)){//same amazing check, credit to Deozaan #Unity3D on IRC
aimPoint = hit.point;
} else {
aimPoint = ray.origin + (ray.direction * 100);
}
projectile = (GameObject)Instantiate( bulletPF, spawnPoint.transform.position, spawnPoint.transform.rotation) as GameObject;
Debug.DrawLine(spawnPoint.transform.position, aimPoint);
Debug.Log("this is hit point: " + hit.point);
//so it is 0,0,0 when i hit nothing
Debug.Log("this is null hit: " + aimPoint);
projectile.transform.LookAt(aimPoint);//send it on the ray
Physics.IgnoreCollision(projectile.collider, Character.collider);// don't hit character
projectile.rigidbody.velocity = projectile.transform.forward * 80;
}
}//end of Update
}//end of class
Your answer
Follow this Question
Related Questions
Help with Camera controls for custom third person controller 0 Answers
Make the terrain ignore Raycast if in between Camera and Player? 1 Answer
Raycast Object Selection 3 Answers
MouseLook movement diagonal stutters... 0 Answers
Mouse Orbit after changing Position sets camera back to last position 1 Answer