- Home /
Firing a bullet doesn't work properly in the bottom half of the screen
I'm using a raycast to guide a bullet trajectory, and it works really well in the top half of the screen. But anything below that fires straight out for some reason. Here is my code:
if (Input.GetButtonDown ("Fire")) {
if (canFire == true) {
if (currentAmmo > 0) {
currentAmmo = currentAmmo - 1;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
ammoType = Instantiate(ammoType, transform.position, Quaternion.identity) as GameObject;
ammoType.GetComponent<Rigidbody>().AddForce(ray.direction * shotPower, ForceMode.VelocityChange);
}
The big problem I'm having is that I want short/crawling enemies on the ground. This is for a lightgun style game.
In this screenshot I'm shoting along the bottom edge of the screen, and the bullet starts in the right position however goes straight out instead of shooting downwards.
I assume that the issue is with Camera.ScreenPointToRay. You might have to create a new direction vector based on the camera's position and the mouse cursor position in world space.
Thanks, I think you are on to something there. I've been struggling with it for about 25 $$anonymous$$utes and cannot figure out a way to do this. I'm more of an audio guy than a coder... would appreciate suggestions!
Answer by permanentrecords · Oct 09, 2015 at 02:37 AM
Turns out I had a bullet spinning animation that was preventing it from changing altitude somehow. Disabled the animation and it is working fine now.
Your answer
Follow this Question
Related Questions
How do I make an Instantiated Projectile travel towards Center of Screen 1 Answer
Raycast off empty gameobject rather then camera. 2 Answers
Positioning a instantiated object using Input.Mouseposition doesn't position right? 1 Answer
inserting random gun into player/enemies hands 3 Answers
Control amount of bullets 2 Answers