- Home /
Raycast problems
Hi all. I am having trouble getting a raycast to go from a ship to the ground when I hold the mouse button down. Here is the script I have so far
var horizontalSpeed = 2.0;
var verticalSpeed = 2.0;
var particle : GameObject;
var myTransform : Transform;
function Update ()
{
// Get the mouse delta. This is not in the range -1...1
var h = horizontalSpeed * Input.GetAxis ("Mouse X");
var v = verticalSpeed * Input.GetAxis ("Mouse Y");
transform.Translate (v, 0, -h);
if(Input.GetMouseButton(0)){
//construct a ray from the mouse point
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if ( Physics.Raycast(myTransform.position, ray)){
Instantiate (particle, transform.position, transform.rotation);
}
//horizontalSpeed=0;
//verticalSpeed=0;
}
}
right now I get an error that says Assets/UFO.js(20,21): BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Vector3, UnityEngine.Ray)' was found. but I'm not sure where the arguments aren't right. how can I get the raycast to go from the ship to the ground? Thanks!
Answer by Berenger · Feb 24, 2012 at 04:27 PM
Raycast have many overload, but basically it needs 1- Origine 2- Direction (and some option, not relevant here. You can either give a ray, which contains those two informations, or the origine and the direction (both Vector3). Try :
if( Physics.Raycast(ray) )
or
if( Physics.Raycast(ray.origin, ray.direction) )
http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenToWorldPoint.html
you have to calculate the distance from the camera to your ground (i am assu$$anonymous$$g you want to point on a ground).
yes I do. is there anyway to send a messege through this raycast to what you hit? I'm trying to do Send$$anonymous$$essege and ray.Raycasthit.Send$$anonymous$$essege but neither of those are working.
Unless the camera never moves, ScreenToWorldPoint will require a raycast to know the distance from the ground anyway.
To click on an object and send him a message, you have two ways.
The unity built-in way : Attach a script on those objects you want to clic, and add the function On$$anonymous$$ouseUp() (or Down), and do what needs to be done.
Your own way : Raycast can give you the collider hit by the ray. Collider is a component, and thus can call Send$$anonymous$$essage. Proceed like that (I am not familiar with UnityScript, there might be mistakes) :
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if( Physics.Raycast(ray, hit) ) hit.collider.Send$$anonymous$$essage( "Hi, how are you ?" ); // Don't send that message :p
I realised the problem is actually that the ray it is drawing is going way off into space no matter where I click it always goes to the same point and it isn't even in the cameras clipping plane. Any idea why this might be?
Answer by Imankit · Sep 27, 2012 at 01:03 PM
if ( Physics.Raycast(myTransform.position,-Vector3.up, ray)){ .....
}
Ushould specify the direction in which you want to cast the ray....