- Home /
Raycast shooting for multiple weapons
I need some advice on how to set-up ray points so i can shoot multiple weapons.
I have 3 + guns. The guns are attached to the FPS capsule in there normal holding position(hip). I have made a simple C# script for shooting. I created an empty cube and placed it infront of one of my guns. In the C# script i used debug and color so i can see the raycast line.
In order for the line to work or should i say "visible" i dont know. Either way, when i hit the f key i had to make the empty cube called "ray point" a child of main camera. Now, this will only shoot from one position.
I only have one camera in my project. From my main camera i have an child empty object named "player weapons" from there its child are my weapons. I dont have a weapons camera.
I am assuming that i need to set up 2 ray cast points(hip and aim) to shoot from?
I have a gun animation that sets it into aiming position.
Im just unsure how i can set this up for each gun and have it follow / shoot wherever its aiming(hip, aim).
public class RayShootAK47 : MonoBehaviour {
public float range = 1000;
public float force = 1000;
public RaycastHit hit;
void Start () {
}
void Update () {
if(Input.GetButtonDown("Fire1")){
Rayshoot();
}
}
public void Rayshoot(){
Vector3 direction_ray = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, direction_ray * range, Color.blue);
if(Physics.Raycast(transform.position, direction_ray, out hit, range)){
}
}
}
Answer by aldonaletto · Aug 30, 2012 at 12:28 AM
If you want to always hit the center of the screen (where you can draw a crosshair), create a ray based on the camera:
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); if (Physics.Raycast(ray, out hit, range)){ // something was hit }This would not work for instantiated projectiles because you could see them starting at the camera position, but works fine for raycast shooting, since you can't see the ray anyway.
I think this could be a good solution for me. thank you.
Your answer
Follow this Question
Related Questions
shooting multiple enemies using raycast 2 Answers
Enemy Shooting Through Raycasts 0 Answers
AI Shooting for Aircraft 1 Answer
Raycast from an objects co-ordinates 1 Answer
Limited ammo from Raycast? 1 Answer