offset problem

Hi guys, I’m having trouble trying to offset a game object and just wondering if anyone can help. I’ve made two vector2 variables called offset and elepos (meaning elephant position) and assigned these the amount I want to offset by and the elephant’s x y position, like this -
offset = new Vector2(5 , 0);
and
elepos = new Vector2(transform.position.x,transform.position.y);
I then want the offset to happen after an if statement by simply saying - elepos += offset;
This isn’t creating any errors but isn’t doing anything, am I doing something wrong? Cheers guys

Here’s a description of what you’re doing:

  • Create a variable called offset, assign it the value of { 5, 0 }
  • Create a variable called elepos, assign it the value of { transform.position.x, transform.position.y }
  • Assign elepos the value of elepos plus offset.
  • Missing Step: transform.position = new Vector3(elepos.x, elepos.y, 0);

If you don’t assign the transform, then you’re not doing anything useful, you’re just playing with structs.

Yep ^ that

‘elepos’ is a reference to the value Vector2(x,y)
‘elepos’ is not a reference to (or controller of) the position of the elephant transform

Cheers guys, that solved the problem. However the addition from offset to elepos happens repeatedly so instead of offsetting once, it keeps adding and the elephant gets further away until it goes offscreen.

Could this be because its in update?

The best way to get help is by posting your actual code here. Don’t forget to use code tags:

I’ve been getting mixed up and using actionscript code -.-
I was incrementing instead of adding.
Thanks for the help guys, been stuck on this for ages and its had my head done in.
Mark