- Home /
Throw Object at Different Angle on MouseClick
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;
}
}
Answer by StartUpStudios · Sep 19, 2016 at 06:11 PM
I've figured out a way to make this work. I took the hit position of a Raycast from the camera to a canvas in front of the camera, and made the 'cannon' look at it. Then shot the object with the cannon's direction. Below is the code. Hopefully it can help someone else out.
using UnityEngine;
using System.Collections;
public class RoadMaster : MonoBehaviour
{
public float firingForce = 500;
public GameObject object2Throw;
public GameObject barrels;
public GameObject Ramps;
public Camera RMCamera;
public GameObject objectCannon;
public Transform mousePOS;
public Transform target;
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);
if (object2Throw != Ramps)
{
GameObject tossedObj = (GameObject)Instantiate(object2Throw, objectPos, objectCannon.transform.rotation);
tossedObj.GetComponent<Rigidbody>().AddForce(objectCannon.transform.forward * firingForce);
} else
{
Instantiate(object2Throw, objectPos, Quaternion.Euler(270, 90, 0));
}
}
//target.position = RMCamera.ScreenToWorldPoint(Input.mousePosition);
//objectCannon.transform.LookAt(target);
RaycastHit hit;
Ray ray = RMCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Vector3 hitPosition = hit.point;
objectCannon.transform.LookAt(hitPosition);
// Do something with the object that was hit by the raycast.
}
}
public void makeBarrels()
{
object2Throw = barrels;
}
public void makeRamps()
{
object2Throw = Ramps;
}
}
Your answer

Follow this Question
Related Questions
Is it recommended to do scientific modelling in unity or should i write a standalone program 3 Answers
Car Movment Script not fully working!!!!!!! 1 Answer
Need help with my movement script.. :( 1 Answer
2D Platformer with custom physics Object - jumping issue 0 Answers
Model not translating along its position with Animate Physics option enabled. 0 Answers