Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3d, Shooting at mouse position

I am creating a simple arcade game however I am having trouble with shooting. The player will have this view. enter image description here I want the player to shoot at the mouse position, from the turret on the player character.

Here's my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class gunController : MonoBehaviour {

    public float speed = 3f;
    public float fireRate = 3f;
    public float force = 10f;
    public GameObject bulletPrefab;
    public GameObject gunEnd;

    private GameObject bullet;
    private Camera mainCam;
    private float distance = 50f;
    private Vector3 mousePos;
    private bulletController bc;
    private bool canShoot = true;
    private Vector3 aim;

    void Start () {
        mainCam = GameObject.Find ("Main Camera").GetComponent<Camera> ();
        mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    }

    // Update is called once per frame
    void Update () {
        //Get mouse position. You need to call this every frame not just in start
        mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);

        //declare your aim by adding +10f to the local.z position of your turret. Not 50f, because if your
        //car goes past 50f then it'll start shooting behind
        aim = new Vector3(mousePos.x, mousePos.y, transform.localPosition.z+10f);
        //rotate turret to mouse
        //now make turret look at your aim not your mousePos - you want it to look at an X Y Z point, not X Y.
        gunEnd.transform.LookAt (aim);
        //get mouse click and asks if we can shoot yet
        if (Input.GetKey(KeyCode.Mouse0)&&canShoot) {
            StartCoroutine(shoot());
        }
    }

    public IEnumerator shoot(){
        //instantiates the bullet
        gunEnd.transform.LookAt (aim);
        bullet = Instantiate (bulletPrefab, gunEnd.transform.position, Quaternion.identity);
        bullet.transform.LookAt (aim);
        Rigidbody b = bullet.GetComponent<Rigidbody> ();
        b.AddRelativeForce (Vector3.forward*force);
        canShoot = false;

        yield return new WaitForSeconds (fireRate);
        canShoot = true;
    }
}

This only shoots backwards, behind the player in the same place regardless of position, or mouse position.

Thank you for reading and hope you can help! :)

like image 769
DavidMcC Avatar asked Dec 20 '25 16:12

DavidMcC


1 Answers

I think the problem comes from this line :

mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);

Because the "Screen position" (Input.mousePosition) does not have any "depth" when you convert it to World space, I suspect the Camera.main.ScreenToWorldPoint to return a wrong value, even if you try to set the value of aim.z some lines after.

Try this instead :

    mousePos = Input.mousePosition;
    mousePos += Camera.main.transform.forward * 10f ; // Make sure to add some "depth" to the screen point 
    aim = Camera.main.ScreenToWorldPoint (mousePos);

By the way, in the code above, Camera.main is used, but you should use the cached version you initialize in the Start function : mainCam

like image 93
Hellium Avatar answered Dec 24 '25 11:12

Hellium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!