I am trying to set up a crosshair for my game. I know I can use a raycast find the position where I click. However I’m having troubles figuring out how to make the bullet move from the player to that clicked location.
As well I can’t figure out what happens if the player shoots at the sky how to get that information for the raycast.
first of all, most games don’t actually send bullets flying though the air, they figure out what they would have shot with a raycast from the gun barrel (or the HUD rect on screen for example) and communicate the damage with it.
you could of course send a rigidbody on a ballistic trajectory simulating a bullet, but it would be a lot of overhead in a bullet hell, it could be useful for sniper action and alike though, where you don’t have much bullets and big spaces.
(you can use multiple raycasts to simulate an arc instead of a straight line)
edit: and you can make it take a bit of time like @Kurt-Dekker mentioned even with raycasts
Thanks guys I got it. Just needed to read this stuff to jog my memory.
if(Input.GetButtonDown("Fire1"))// && Timer <=0)
{
Ray ray;
RaycastHit hit;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Target = Instantiate(HitPoint, hit.point, Quaternion.identity) as GameObject ;
Bullet = Instantiate (Spell, gameObject.transform.position, gameObject.transform.rotation) as GameObject ;
Bullet.transform.LookAt (new Vector3 (Target.transform.position.x, Target.transform.position.y, Target.transform.position.z));
//Timer = TimerAmount;
}
}
I ended up doing this, and putting invisible colldiers in the sky, so the player could shoot there. My last problem was I was forgetting that it’s the tip of the cursor is where the hit information is added. So I just moved my crosshairs. Thanks guys a lot
If you want to detect specific case when player shooting in the sky, place invisible box collider up there and if raycast hitted that collider, then you can be sure player was targeting the sky.
*@*aljstevens “I ended up doing this, and putting invisible colldiers in the sky, so the player could shoot there.”
You can also define maximum distance for a raycast, if you don’t need to know for sure that you hit sky like @palex-nx said.
Set raycast maximum distance to something big that suits your game level, if your ray hits before this, you have an environment hit, if not, you are aiming is towards sky.
if (Physics.Raycast(shootingRay, out bulletHit, hitDistance, raycastLayer))
{
// hit environment
}
You should always use maximum distance for performance. There is no reason to use colliders in sky for this.
I’m working on ballistics on and off for our game. You can shoot straight up and the bullet will return thanks to gravity . Need to implement steam achievements just for the achievement to kill a enemy player with a returning bullet
That’s exactly the problem, instead of allocating new room for a new object and cleaning up old ones, you re-use the old ones saving time, it does require what ever is being pooled to be able to revert back to it’s original(“prefab”) state.
look into it, it’s worth while, but know where to apply it.
Make sure cross hair at center of screen.
make sure camera is also in center of screen
Here is the shoot function
RaycastHit hit;
if (Physics.Raycast(instantiationSpot.transform.position,mainCamera.transform.forward, out hit, 2500,layerMask))
{
// Debug.Log(hit.collider.name+“has been shot”);
// bullet instantiation
Rigidbody bulletClone = this.GetComponent().getBullet(instantiationSpot.transform.position,Quaternion.LookRotation(hit.normal)).GetComponent();
//setting velocity such that it reaches destination
bulletClone.velocity =( hit.point - instantiationSpot.transform.position ).normalized * 10;