x


up and down detect

how in script to detect if the object was moved within y axis?, i have a gameobject that should be moved up and down

more ▼

asked Feb 13 '12 at 04:58 AM

charnew gravatar image

charnew
31 15 18 23

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

In general, store the last position in a variable and check if the distance from the current position is greater than an epsilon. In your case, just do it with the y component.

more ▼

answered Feb 13 '12 at 05:02 AM

Berenger gravatar image

Berenger
11k 12 19 53

is this how i should do it?

var lastposition : (whatsthedatatypeforthis?);

var transform.position.y = 0; if (lastposition != transform.position.y){ Debug.log = "moved" }

Feb 13 '12 at 05:19 AM charnew

If you just want a test on the height, it's a float then. Something like :

var lastHeight : float; // renamed to be more coherent

function Update()
{
// It's better to do it that way, float comparison can give you surprises due to lack of precision.
if( Mathf.Absolute( lastHeight - transform.position.y ) > 0.1F )
{}// Here the height changed, to whatever you need to
lastHeight = transform.position.y;
}

Feb 13 '12 at 06:29 AM Berenger
(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:

x38
x29
x4

asked: Feb 13 '12 at 04:58 AM

Seen: 324 times

Last Updated: Feb 13 '12 at 06:30 AM