Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by forferdeilig · Sep 19, 2013 at 03:23 AM ·

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,

  1. on line 11 I get "bce0019: 'transform' is not a member of 'UnityEngine.vector3'.

  2. I'm not sure that will give me the distance i want, as i only want distance in the global y axis.

  3. How is it possible to do the following: if(distance < hoverHeight, set distance to = hoverHeight at a certain rate;

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadoX · Sep 19, 2013 at 07:47 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Creating an Object of another class inside a class 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges