x


Following Terrain Height?

EDIT: THE PROBLEM IS HIT.DISTANCE AS IT KEEPS CHANGING. I need it to be dynamic, but not be 2 things at once.

Is there a way to make an object go all the way down on the Y axis until it hits the terrain? As well as rotate with the curves?

I do not even know where to start with it , also I use javascript instead of C#, as I do not really know c#.

Extra info: I am instantiating an object, but using the code below, it just starts flickering from the instantiated place to the area the code calls for. The code below is all that is attached to the instantiated object.

Here is the code:

function Update () { var HighestPointOfTerrain : int = 50; var hit : RaycastHit; if(Physics.Raycast(transform.position, -Vector3.up, hit)) { var distancetoground = hit.distance; var heightToAdd = transform.localScale.y; transform.position.y = HighestPointOfTerrain - distancetoground+ heightToAdd; } }

more ▼

asked Jan 02 '12 at 06:26 AM

brandon199511 gravatar image

brandon199511
16 9 9 12

If you are looking for having it stay flush with the terrain, you will want to add a rigidbody component to the object.

Jan 02 '12 at 07:19 AM rabbitfang

There is one added already.

Jan 02 '12 at 07:22 AM brandon199511

So then what is the problem? If the object has physics, it should stay on flush with the terrain automatically.

Jan 02 '12 at 07:25 AM rabbitfang

I don't know any other way of saying that it flickers in two places

Jan 02 '12 at 07:27 AM brandon199511

By 'flickers' do you mean that is moving to multiple positions very rapidly?

I think the issue might be with the script moving the object to a bad location and then the physics engine pushes the object out of that location, the script puts it in another bad spot, and so on.

Jan 02 '12 at 07:34 AM rabbitfang
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Try changing the line

transform.position.y = HighestPointOfTerrain - distancetoground+ heightToAdd;

to

transform.position.y = transform.position.y - distancetoground + heightToAdd;

Basically, what was happening was you were assuming that your object was always at a height of HighestPointOfTerrain (at least, that is what the code was doing), which caused your calculations to get messed up.

more ▼

answered Jan 02 '12 at 07:42 AM

rabbitfang gravatar image

rabbitfang
912 2 4 11

You my friend are a life saver! Thank you so much!

Jan 02 '12 at 07:54 AM brandon199511

Then please mark my answer as correct (look for the check mark).

Jan 02 '12 at 04:28 PM rabbitfang
(comments are locked)
10|3000 characters needed characters left

are you looking to put an actual object on the ground or is this like a targeting box or something along those lines that you want to simply project onto the terrain. if it's the second idea, you might want to look into projections.

http://unity3d.com/support/documentation/Components/class-Projector.html

more ▼

answered Jan 02 '12 at 08:01 AM

kievar1983 gravatar image

kievar1983
66 9 11 15

The previous answer got what I needed. Works like a charm now :).

Jan 02 '12 at 08:16 AM brandon199511
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1530
x1473
x498
x194
x94

asked: Jan 02 '12 at 06:26 AM

Seen: 1306 times

Last Updated: Jan 02 '12 at 04:28 PM