x


Unable to move object upwards at exact point - Unity

I am new in unity. Here is my scenario which I want to implement.

I have a enemy and a tower. Enemy collides with tower and move to the top of tower to escape from it. I am unable to move enemy at exact position of towers top.

here is my code:

void OnTriggerEnter(Collider collider) {

  moveY = tf.position.y + 6; //6 is the height of tower

  tf.Translate(0, moveY, 0, Space.World);

}

If I use position instead of translate it moves to towers top position, but I want to walk the enemy towards top of tower. Using translate moves enemy more towards bottom. Pls help me with this

more ▼

asked Sep 28 '11 at 09:38 AM

haroonalam gravatar image

haroonalam
65 25 35 36

You shouldn't be defining moveY in terms of the current position- Translate moves the object by a set amount, so you should just translate it by the amount you want it to move. I don't quite understand your code here- why is it inside an OnTriggerEnter?

Basically, you shouldn't have the tf.position.y - you should just translate up by 6.

Sep 28 '11 at 09:44 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Like syclamoth is saying, just use 6 as moveY since Translate is simply adding this vector to the current position. So moving your enemy up with 6 units from its current location is

tf.Translate(0, 6, 0, Space.Self);

(* I'm using Space.Self because I assume your enemy has it's own up vector pointing to where it wants to climb *) You can also set the world position by

tf.position += new Vector3(0, 6, 0);
more ▼

answered Oct 01 '11 at 09:12 AM

Martijn Hendriks gravatar image

Martijn Hendriks
101 3 4 9

(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:

x4172
x3747
x337

asked: Sep 28 '11 at 09:38 AM

Seen: 655 times

Last Updated: Oct 01 '11 at 09:12 AM