Bullet to Mouse Position?
Hi;
I've got some code that only fires in a straight line and I can't figure out for the life of me how to get it to the mouse position. Can anyone help ?
public Rigidbody projectile;
public float speed = 20;
void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
var pos = Camera.main.WorldToScreenPoint(transform.position);
var dir = (Input.mousePosition - pos).normalized;
Rigidbody instantiatedProjectile = Instantiate(projectile,
transform.position,
transform.rotation)
as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
}
Comment
Answer by say_forever · Nov 24, 2015 at 02:35 AM
Ray rayFromCam = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(rayFromCam, out hitFromCam))
{
hitFromCam.point //this method return Vector3
}
Hope this is what you need
all seems fine, but the hitFromCam.point is throwing up an assignment, call error
because it would return Vector3 to let you use. like show information Debug.Log(hitFromCam.point);
How would this be fixed then? Sorry I'm kind of a program$$anonymous$$g 'noob' and just trying to understand it at the same time
first, you double define RaycastHit hit; then, hit.point will return a vector3, so you need to use it.