- Home /
Distance between 2 objects 1 axis
How can you get The distance, This script is meant to make a vehicle hover, but i really don't know anything about coding, all i've done was a tiny bit of vb a very long time ago. New script
#pragma strict
function Start () {
}
var hoverHeight : float = 5;
function Update ()
{
var groundLoc = GameObject.Find("ground").transform.position;
var distance = Vector3.Distance(this.transform.position, groundLoc.transform.position);
if(distance < hoverHeight, set distance to = hoverHeight at a certain rate;
{
}
}
the problems i need to fix with this are,
on line 11 I get "bce0019: 'transform' is not a member of 'UnityEngine.vector3'.
I'm not sure that will give me the distance i want, as i only want distance in the global y axis.
How is it possible to do the following: if(distance < hoverHeight, set distance to = hoverHeight at a certain rate;
Regarding line 11.. the problem is that you're tring to get a transform from 3D coordinates.
var groundLoc = GameObject.Find("ground").transform.position;
var distance = Vector3.Distance(this.transform.position, groundLoc.transform.position);
Problem being at groundLoc.transform.position
groundLoc is a "position" of a transform which means that it's a Vector3 variable (x, y , z)
Basically you're trying to do too much .. you're trying to get the Vector3 from a variable that already contains it... so ins$$anonymous$$d of
var distance = Vector3.Distance(this.transform.position, groundLoc.transform.position);
it should be
var distance = Vector3.Distance(this.transform.position, groundLoc);
"var distance" will contain a float, but it will be the distance between the two specified points http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Distance.html
if the two points are at the exact same x,z coordinates and the only difference is y then the result is pretty much what you want, but if the two points don't overlap in (x,z) then you probably would want to find a way to calculate the y distance in some other way.
For example, you could do something like http://www.csgnetwork.com/righttricalc.html assu$$anonymous$$g that A and B are the two points and a is the desired distance that you're looking for.
Answer by Turkeybag · Sep 19, 2013 at 08:05 AM
I don't think that whole thing will work as the transform.position will always be the origin of the ground rather than the part of the ground directly below the vehicle.
You'd be better off using raycasting to detect the distance. Something like this (untested but should work):
var hoverHeight : float = 5;
function Update ()
{
if (Physics.Raycast (transform.position, Vector3(0,-1,0), hoverHeight))
{
rigidbody.AddForce(Vector3(0,10,0));
}
}
It will pretty much move the vehicle up with a force of 10 if anything's 5 units below it.
Answer by robhuhn · Sep 19, 2013 at 08:04 AM
You might find your answer here: http://answers.unity3d.com/questions/252737/hovering-tank.html
Your answer
Follow this Question
Related Questions
what is the problem of this code ? 2 Answers
Make Deaths end game 1 Answer
JS to C#, anyone? 1 Answer
Audio not playing [Fixed] 1 Answer