I’m making a top down shooter game. Currently I’m having an issue when I click to shoot sometimes the bullet flies to the top of the screen, instead of hitting the floor. Anyone have any suggestions how to fix this?
Ray ray;
RaycastHit hit;
public GameObject prefab;
public GameObject Bullet;
public GameObject Player;
void Update ()
{
ray=Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit))
{
if(Input.GetButtonDown ("Fire1"))
{
GameObject obj=Instantiate(prefab,new Vector3(hit.point.x,hit.point.y,hit.point.z), Quaternion.identity) as GameObject;
}
}
}
Hello, I tested your code in an empty scene and as you can see it’s accurately placing the spheres where my mouse pointer is clicking. Since you are claiming that the problem is they are shooting at the top of the screen I feel like there’s another script you haven’t showed us that is the source of the issue.
Ok I found out what is causing it. It’s a trigger collider on an enemy. I have it set to a trigger so I don’t understand why the objects are appearing on it.
Well without seeing what components are attached to your objects and such. Anything with a RigidBody will apply physics if they are not set to kinematic so that would cause things to bounce around if the settings aren’t done properly.
Without a RigidBody objects will not follow the laws of physics or gravity. If you can give me more information I can narrow down the problem.
I was doing some testing and it seems like even a gameobject with just a Collider attached will make the prefabs appear on it.
In my picture below is just empty game object with the collider on it. And if you look around the edge of the colldier you can see the prefabs floating there. Also I want to add I set it to a trigger as well, I’m still having the same problem
Ohhhh I get it. Basically you are saying that your prefab is appearing on top of the enemy object instead of on the ground below them?
If that’s the case then you are going to have to set a new layer for the enemy object call it Enemy Physics then go to Edit > Project Settings > Physics
Set the enemy layer Enemy Physics to ignore raycast and make sure you apply this layer to your enemy prefab. Now the object will no longer spawn on top of the enemies since the raycast is ignoring them.
This will turn off all raycast detection on the enemy though so if you need them to be raycasted in any game, there ARE other workarounds to your problem. I just feel like this is the easiest workaround for what you are doing with your game.
In this photo you linked before, try putting the Enemy Physics layer on it too. It looks like the Raycast is colliding with the sphere collider here too.
That doesn’t seem to fix the problem either. I even tried to put everything on the Enemy Physics layer and I could still shoot it. Than I attempted to have all the layers be ignored and I could still shoot. This is really odd.
public GameObject prefab;
public GameObject Bullet;
public GameObject Player;
void Update()
{
Vector3 mousePosTest = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
Ray ray = Camera.main.ScreenPointToRay(mousePosTest);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (Input.GetButtonDown("Fire1"))
{
Vector3 hitPoint = new Vector3(hit.point.x, hit.point.y, hit.point.z);
GameObject obj = Instantiate(prefab, hitPoint, Quaternion.identity) as GameObject;
}
}
}
Okay I tested the poop out of this and it works. I don’t understand why the Enemy Physics layer isn’t ignoring raycast I’ve tried it with Ignore Raycast checked and unchecked.
Change your player, the enemy, the sphere AND the prefab to the “Ignore Raycast” layer. That will work.
I made some changes to the code such as constantly reassigning the hit in update since Camera.main.ScreenPointToRay your camera is the one projecting your raycasts and since you have the camera set to follow the player, it means that the camera position is always changing so I’d update the Raycast Hit at all times.
Basically the prefab was instantiating on top of itself, on top of the player and on top of the enemy AND the sphere, that was why it kept appearing above everything.
There are ways to make this work without using ignore raycast so if you need the enemy, player or your landmines or whatever they are to be detected by raycasts just reply here and I’ll tell you how to do that but it MIGHT require you to make some compromises to how you want your game to work, we’ll see.
Thank you that fixed my issue. You are beyond amazing. I really appreciation the help you gave me. Like really you are great! If I can do something for you please let me know.