Shoot balls from mouse position (Solved)

Hi, i found this script that i want to use when u click screen it instantiate a prefab and shoot it from the mouse cursor, the problem i have is that it always shoot straight forward.

I want that if i click in midle it goes straight forward but if i click the corners the ball shoot diagonaly so it looks like object come from middle of screen and get shoot towards wherever you have the mouse.

this is the script any hepl is appreciated.

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

public class Shoot : MonoBehaviour
{

    public float spawnHelper = 4.5f;
    public GameObject ball;
    public float ballForce = 700;

    private Camera _cam;


    void Start()
    {
        _cam = GetComponent<Camera>();
    }


    void Update()
    {

        //Local floats
        float mousePosx = Input.mousePosition.x;
        float mousePosy = Input.mousePosition.y;

        Vector3 BallInstantiatePoint = _cam.ScreenToWorldPoint(new Vector3(mousePosx, mousePosy, _cam.nearClipPlane + spawnHelper));


        if (Input.GetMouseButtonDown(0))
        {
            GameObject ballRigid;
            ballRigid = Instantiate(ball, BallInstantiatePoint, transform.rotation) as GameObject;
            ballRigid.GetComponent<Rigidbody>().AddForce(new Vector3(ballForce, 0,0));
        }
    }
}

The force being added on line 35 is what is basically setting the direction the ball moves. I’d probably use transform.LookAt to rotate the ball towards the point where you clicked your mouse, then add a relative force to the ball along its z axis. Or something along those lines.

Asked an asset developer Gustav Olsson who had this as an helper script in one of his asset he was kind enough to give me the script for free and gave me permission to share it here incase anyone else would need it and thanks to Gustav for sharing it.

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

public class MouseInstantiate : MonoBehaviour
{
    public GameObject prefabToInstantiate;
    public float speed = 7.0f;

    public void Update()
    {
        if (prefabToInstantiate == null)
        {
            return;
        }

        if (Input.GetMouseButtonDown(0))
        {
            Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            GameObject newGameObject = (GameObject)Instantiate(prefabToInstantiate, mouseRay.origin, Quaternion.identity);
            Rigidbody rb = newGameObject.GetComponent<Rigidbody>();

            if (rb != null)
            {
                rb.velocity = mouseRay.direction * speed;
            }
        }
    }
}
2 Likes

Were do you put the script on the pistol ore creat something like a ball new to unity its why

Instead of starting with someone else’s two-year-old thread, please start your own, it’s FREE.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

If you are unsure where to place a script, check out some basic Unity tutorials, such as these:

Imphenzia / imphenzia - super-basic Unity tutorial:

https://www.youtube.com/watch?v=b3DOnigmLBY

https://www.youtube.com/watch?v=pwZpJzpE2lQ

Brackeys super-basic Unity Tutorial series:

https://www.youtube.com/watch?v=IlKaB1etrik

Sebastian Lague Intro to Game Development with Unity and C#:

https://www.youtube.com/watch?v=_cCGBMmMOFw

Basics of Unity GameObjects and Components:

https://www.youtube.com/watch?v=9Nf2_ds5y8c

Put it on the camera, and make a prefab sphere with a rigidbody on.