- Home /
Shooting projectiles to the position of the mouse click?
I'm currently making a 3D top-down shooting game for my school project and I want the player character's projectiles to go to the direction of the mouse click, but right now, no matter where I click, the player's projectiles always go to the front direction of my character like this picture.
Does anyone know what's wrong with my script? Here is the shooting script for my player character.
public class shooting : MonoBehaviour { private GameObject player; public GameObject projectile; public float shootingForce; public AudioClip shootingSound; private AudioSource shootingAudio;
void Start()
{
player = GameObject.Find("Player");
shootingAudio = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
GameObject shot = GameObject.Instantiate(projectile, transform.position, Quaternion.identity);
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);
}
}
Your answer
Follow this Question
Related Questions
How to double spirte/gameobject/prefab and control the result on those items? 0 Answers
How can i move the camera smooth like flow up and behind the player when clicking the escape key ? 0 Answers
The spaceship acceleration script is good ? And how to use it with engine ? 0 Answers
How do I make the enemy script/AI stop following me when It is in view of the player camera? 1 Answer