- Home /
raycast and instantiat advice
I am about to start production on a new game where the code below is one of the main functions. I learned how to write it from a tutorial and just came back to it. I made a little tech. demo works nicely the raycast finds a the object you click with mouse and instantiates a bullet from another object.
My only worry is when I click on objects further away from the inatantiated prefab the velocity seems to be much greater than closer objects! A little insight would be very welcome before starting! Thank you in advance!
if(Input.GetMouseButtonDown(0)){
Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitinfo;
if(Physics.Raycast(rayOrigin, out hitinfo, distance)){
// Var for third person raycasting
// Also part of third person raycasting.
var direction = (hitinfo.point - actor.transform.position);
// var testray = new Ray(actor.transform.position, direction);
// Draws ray from 'actor' var to mouse click.
// Seems to draw multiple rays at the moment. I think from the camera.
Debug.DrawRay(actor.transform.position, direction);
Debug.Log("You are casting a ray");
Debug.DrawLine(rayOrigin.direction, hitinfo.point);
// Instantiate.
Rigidbody clone;
clone = Instantiate(bullet, actor.transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection(direction * speed);
you need to normalize your direction vector. Otherwise it will have the magnitude of the distance between the two points it was calculated from.
after looking at it I realized since the distance I am shooting becomes greater this multiplys by the speed. I am shooting in the x direction so I guess if I subtract that value of direction from speed it would make it a little better?
@aronatvw - The issue you outline in your comment is the same one that @Benproductions1 addresses with the need to 'normalize your direction vector:
var direction = (hitinfo.point - actor.transform.position).normalzied;
awesome guys thank you I've never heard of that. I am going to try it. Also I will debug.log and look and the script reference but any other insight before hand on what it actually does?
Thank you guys that worked out perfectly. So from what I understand any "distance" between the two points is set to one. Regardless it works perfectly thank you much!