Raycast problem (401318)

hi guys,

I am stuck at a problem, i have added a collider to a object and when the object is touched it is set in to motion, the problem is that even if the user touches the area around the object it is set in to motion. I have checked the collider area for the same it is restected to the object.
I have used the ray-cast code as provided as it is but it works in one project using “js” but fails in the other when i am using “cs”.

Following is the code i am using to have a look at

if( Input.GetMouseButton(0) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray,hit)){
var filter: CapsuleCollider = hit.collider.GetComponent( CapsuleCollider );
if(filter== transform.GetComponent( CapsuleCollider ) ){
// move object  
}
}

error 1: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments

error 2 :Use of unassigned local variable `hit'

thank you:)

Maybe if you create thread that is at least half-decent people would want to help you. Use the preview button, fix your post.

And if you want your problem solved just take a look at the script reference: Physics.Raycast

hey Zephyrus

edited the post have a look :slight_smile:

The main difference in syntax with Physics.Raycast in C# is that you need to put the word “out” in front of the RaycastHit parameter. This is because the function sets the value of this variable during the call.

@andeeee: Thanks a lot…tht helped.

Now I am facing another problem. I cannot see the ray drawn using the Debug.DrawRay() function. Also the code never goes inside the if statement.

I have attached the code.

Please help.

RaycastHit hit;
ray = Camera.main.ScreenPointToRay(new Vector3(Y.x,Y.y,Y.z));
		Debug.DrawRay(Y,this.transform.forward,Color.red);
//		ray = new Ray(Y,Vector3()); 
		Debug.Log("YYYYYYYYY"+ray);
		if (Physics.Raycast (ray,out hit)){
				Debug.Log("in if ..........");
				Component filter =  hit.collider.GetComponent("BoxCollider");
				Debug.Log("filter......"+filter);
				if(hit.collider.GetComponent( "BoxCollider" ) == transform.GetComponent( "BoxCollider" ) ){
					Debug.Log("Box Collider");
	 		}
		}

Is this code positioned in the Update method? If this is not the case, the ray will only be visible for one frame, so you will not be able to see it.

The object with the collider must have a rigidbody component for the raycast to work.

thanks for the help but FYI

  1. No the raycast code is not written in the update method its written in a separate function which is called whenever some1 touches on iPhone.

  2. Yes the GO with d collider has a rigidbody component attached to it.

  3. What should i do to make the ray visible throughout or for a longer period.

4)How do i ensure that the ray is going in the GO’s direction.
5) How the ScreenPointToRay works or how to use it.

  1. i have a GO with collider and rigidbody attached to it and i want it to move only when it is touched.

ne code snippet will be helpful

Have a look at this thread for a way to keep a ray visible for a while after the raycast.