- Home /
How would I declare a variable that stores starting position of a GameObject?
I'm trying to make a 2D platformer where a GameObject mirrors the player. (New to programming - artist)
I'm trying to declare a variable to store the starting position of the player, and then I have to declare a variable that stores any offset position from the starting position. Then, I need to declare a variable to store the starting position of the GameObject. Finally, I have to negate the offset position of the player from the starting position of the GameObject (for a mirroring effect).
Is there any easier way? Am I doing something wrong? Here's my attempt at making the first script to put on the player (stores the starting position and the offset position). Sorry if it is bad, like I said, I'm pretty new to programming.
This is what I have:
var initialPosition : Vector3;
var offsetPosition : float;
function Start()
{
initialPosition = transform.position
}
function Update()
{
offsetPosition = initialPosition - (what should I put here??)
EDIT: I've got the mirroring to work thanks to POLYGAMe and Alex, but now the object being mirrored doesn't seem to react to gravity. It will float if the player is at a higher altitude, and it will go through walls (and sometimes go through the player). Here is a video of the problem:
You're trying to set the offset position, which is a float, to a Vector3, which stores three separate floats (one for X, one for Y, and one for Z).
You could try creating a Vector3 for the offset position and doing say vOffsetPosition.x = (vInitialPosition.x - 5,0). I added the little vs in there so that you instantly know you're using vectors, not floats. Helps to name variables properly :)
Oh, by the way, if you do that in update, it calls the command every frame, so you'll find it will shoot to the right...
You'll also need to set the transform position of the object you want offset to that offset position. You could use global variables... bit naughty, but hey... I do it all the time as a noob myself ;-)
Answer by hathol · Jul 01, 2012 at 04:15 AM
Depends on the axis over which you want to mirror your player. In case it is the x axis (left to right) and the centre of your game world is at Vector3.zero your code could be something like
var centrePosition : Vector3 = Vector3.zero;
function Update()
{
mirrorPos = transform.position;
mirrorPos.x = centrePosition.x + (centrePosition.x-transform.position.x);
mirrorObject.transform.position = mirrorPos ;
}
I've noticed that the object isn't affected by gravity now. The object will go through walls and such and will float if the player is on a higher altitude. Is there any way to fix this? I've tried adding a rigidbody,however the mirrorObject isn't affected by it.
If you want to use physics, the concept basically stays the same. You'll have to add a rigidbody to both of your objects, mirror the velocity (or whatever you use to get it moving) and apply it to the other object as well. So along the lines of
function Update()
{
var baseVelocity : Vector3 = Vector3.right;
mainObject.rigidbody.velocity = baseVelocity;
baseVelocity.x *= -1;
mirrorObject.velocity = baseVelocity;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613073533im_/https://answers.unity.com/themes/thub/images/avi.jpg)