Hi
I got a problem with my scipt it doesnt work how it shood
What i want it to do is to Check the Distance betweend the [ThePlayer] [TheObject] so when [ThePlayer] is near [TheObject] and you press (E) a new LevelLoads
Heres The Script i got so far
var thePlayer : Transform;
var theObject : Transform;
if (( Vector3.Distance(thePlayer.transform.position, theObject.transform.position) <= 20.0f ) Input.GetKeyDown("e"))
{
Application.LoadLevel(2);
}
Thanks in advance
Did you put the code between a function?
Like this?
var thePlayer : Transform;
var theObject : Transform;
function Update()
{
if (( Vector3.Distance(thePlayer.transform.position, theObject.transform.position) <= 20.0f ) Input.GetKeyDown("e"))
{
Application.LoadLevel(2);
}
}
In addition to your code needing to be inside a function (Update is fine, although you don’t really need to check every single frame) you probably want to attach this script to either the player or the object you’re comparing him too as a first step. Also, you’re not putting anything into the two variables you’ve defined so there’s nothing to compare.
Here’s a suggestion: search the forums or use google and you’ll find threads like http://forum.unity3d.com/threads/52256-newbie-check-distance-between-two-objects where this has already been discussed. There’s some code in there that might help you, including how to actually load the correct game object or transform into your variables.