- Home /
Raycast to Terrain (Conditional Statements)
I have been trying to get my helicopter's raycast line to collide with the terrain so that when the line collides with the terrain, something happens. For now I just put print for testing/debugging purposes.
I do not have a lot of experience with this Ray-casting business. What am I doing wrong?
Here is my code:
#pragma strict
public var Player : GameObject;
public var Follow : GameObject;
public var terrain : Terrain;
function Update () {
var hit : RaycastHit;
Debug.DrawRay(transform.position, transform.up * 3, Color.red);
if(Physics.Raycast(transform.position, transform.up, hit, 3)) {
if(hit.collider.gameObject == terrain){
print("You hit the Terrain.");
}
}
}
Answer by BMRX · Apr 30, 2015 at 09:28 PM
I don't understand how the Javascript side works, but it looks like you have not assigned your terrain variable.
In C# I would have to do something like:
GameObject Terrain = GameObject.Find("Terrain");
Right now it appears that your code is looking for a gameobject that you have called "terrain" but it still does not know if you want a specific gameobject called Terrain.
You assign public variables in the inspector. This is no different in C#.
I guess you could do that. I have yet to use the inspector in such a way.
He might be better off having the cast check for a terrain tag.
In any case it looks like the cast really has no idea what it's looking for.
Yes, as stated from Eric5h5, I have already assigned all correct things from the inspector. I really can not seem to figure this out..
All I want is when that raycast line hits the terrain, I want it to { Do Something } which I will code after I have got the collision working between the raycast and the terrain.