I have a script where I click and an object spawns on the screen (in first person). Great. However, no matter where I click on the screen, it’s always throws perfectly perpendicular to the camera, effectively making it impossible to aim. I want to throw as if there was a cannon attached to the camera, and it pointed to wherever the mouse was in the world.
(Yes, I know I dont have any ‘throwing’ physics on my Inst obj. But I figured I would get the angle down before I messed with the throwing physics)
I could not find a single thing. I could have been using the wrong search terms, but my past 2 hours of searching has not netted me an answer. Thank you for your time.
Here is the code I use:
using UnityEngine;
using System.Collections;
public class RoadMaster : MonoBehaviour
{
public GameObject object2Throw;
public GameObject barrels;
public Camera RMCamera;
public Transform gunObj;
Plane plane;
void Start()
{
plane = new Plane(Vector3.forward, Vector3.zero);
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Debug.Log("Firing");
var mousePos = Input.mousePosition;
mousePos.z = 10.0f; // we want 2m away from the camera position
var objectPos = RMCamera.ScreenToWorldPoint(mousePos);
Instantiate(object2Throw, objectPos, Quaternion.LookRotation(RMCamera.ScreenToWorldPoint(mousePos)));
}
}
public void makeBarrels()
{
object2Throw = barrels;
}
}