- Home /
How to shoot a bullet to the mouse cursor position?
I'm currently making a 3d top-down shooter and I want the player to shoot bullets to the position of a mouse click, but right now no matter where I click, the bullets always go on top direction. Here is my shooting bullet script so far: public class shooting : MonoBehaviour { public GameObject projectile; public float shootingForce; public AudioClip shootingSound; private AudioSource shootingAudio;
// Start is called before the first frame update
void Start()
{
shootingAudio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
// GameObject shot = GameObject.Instantiate(projectile, transform.position, transform.rotation);
// shot.GetComponent<Rigidbody>().AddForce(transform.forward * shootingForce);
GameObject shot = GameObject.Instantiate(projectile, transform.position, transform.rotation);
Vector3 screenPoint = Camera.main.ScreenToWorldPoint(transform.position);
Vector3 direction = (Input.mousePosition - screenPoint);
direction.Normalize();
shot.GetComponent<Rigidbody>().AddForce(direction*shootingForce, ForceMode.Impulse);
shootingAudio.PlayOneShot(shootingSound, 1.0f);
}
}
} Does anybody knows how to fix this?
Answer by mf41z · Jan 14 at 09:09 AM
You mean the bullet goes up? 'the bullets always go on top direction' Didn't get this part. What you should be doing is using Raycast if you want the bullet to hit what you're pointing at specifically. First get a ray that goes out from your camera to the mouse position:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
The end of that ray will be where you're looking at.
Vector3 dir;
if (Physics.Raycast(ray, out RaycastHit hit, 30f))
{
dir = hit.point - player.transform.position;
}
That will get you the point where the player is looking at, rather than the cursor position overall. Use that direction to apply the force at the end. Tell me if that works for you or if you have any questions
UPDATE: I tried to fix my own code by following your instructions but I got the error that says "shooting.cs(31,35): error CS0120: An object reference is required for the non-static field, method, or property 'Component.transform'". Here is my code so far.
public class shooting : MonoBehaviour { public GameObject projectile; public float shootingForce; public AudioClip shootingSound; private AudioSource shootingAudio;
// Start is called before the first frame update
void Start()
{
shootingAudio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
GameObject shot = GameObject.Instantiate(projectile, transform.position, transform.rotation);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 dir;
if (Physics.Raycast(ray, out RaycastHit hit, 30f))
{
dir = hit.point - player.transform.position;
shot.GetComponent<Rigidbody>().AddForce(dir * shootingForce, ForceMode.Impulse);
}
shootingAudio.PlayOneShot(shootingSound, 1.0f);
}
}
Can you explain to me how to fix that?
@mf41z UPDATE 2: I fixed my code a little bit and I got two problems. 1) The bullets are moving very slowly no matter how much shooting force I applied to my script. 2) The bullets goes on random directions instead of following the mouse click. Here is my code so far. public class shooting : MonoBehaviour { public GameObject projectile; public float shootingForce; public AudioClip shootingSound; private AudioSource shootingAudio;
// Start is called before the first frame update
void Start()
{
shootingAudio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
GameObject shot = GameObject.Instantiate(projectile, transform.position, transform.rotation);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
shootingAudio.PlayOneShot(shootingSound, 1.0f);
Vector3 dir;
if (Physics.Raycast(ray, out RaycastHit hit, 30f))
{
dir = hit.point - transform.position;
shot.GetComponent<Rigidbody>().AddForce(dir * shootingForce, ForceMode.Impulse);
}
}
}
Can you tell me how to fix this?
Your answer
Follow this Question
Related Questions
Problem with firing laser in direction of camera 1 Answer
Shooting and reloading mechanism problem. 1 Answer
How to shoot with swipe? 0 Answers
FPS rigidbody bullets not moving to center of screen 0 Answers
Shoot Script Help 1 Answer