Dist = Vector3(player.position, finish.position); Wont work on droid

var player : Transform;
var finish : Transform;
var dist : float;

function Update(){
dist = Vector3(player.position, finish.position);
}

This simple code is really holding up my game. I need to know the distance from player to finish line. This code works great in Unity, and Unity Remote but when i build and run its always 26000…any reason? Thank You for any help.

That’s because Vector3 does not have a constructor that takes 2 Vector3s

change it to this:

dist = Vector3.Distance(player.position, finish.position);

for reference, 2D distance done in code looks like this:

function Dist2D( x1:float , y1:float , x2:float , y2:float ) :float
{
	var dx:float = x1-x2;
	var dy:float = y1-y2;
	return Mathf.Sqrt(dx*dx+dy*dy);
}

Just for reference/curiosity :slight_smile:

sorry i did have Vector3.Distance, I hand typed that so sorry about that.

I don’t know exactly about vector3.Distance, but you can get the same value doing so:

dist = Vector3(player.position - finish.position).magnitude

Akta,
dist = (player.position - finish.position).magnitude; works great!! Had to drop the Vector3 at the begining though. Thank You for the help.

no problem, you have to thank maths, not me :slight_smile: I think basically the Distance() method does exactly the same, is just a shortcut for what I wrote

I know this is changing the topic here but was wondering if you could help me with another problem I’ve been having. When First loading a scene on android everything runs great but then I get to a new object and it stutters a bit then resumes running fine again. Is there a way to front load all of that stuff to process during level loading. Ex space ship game runs fine until i hit throttle then fire starts and stutters then runs fine even with the fire there. Thanks dont know how good this explaination was but thanks for anything.

yeah I noticed that as well the first time you instantiate some new object. I don’t really know if there is a way to improve that. Try to do one thing. When the scene loads put the object you instantiate somewhere you can’t see it and destroy it in its Start() phase. It might be that the engine allocates that object in the memory in advance, so the next time you try to instantiate that it should be smooth and fast, but this is just a guess, haven’t tried myself.