Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jasoncosma · Jul 01, 2012 at 03:50 AM · transformposition

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:

http://youtu.be/asBZZ944Wxo

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image POLYGAMe · Jul 01, 2012 at 04:14 AM 0
Share

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

avatar image POLYGAMe · Jul 01, 2012 at 04:15 AM 0
Share

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...

avatar image POLYGAMe · Jul 01, 2012 at 04:18 AM 0
Share

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 ;-)

avatar image jasoncosma · Jul 01, 2012 at 11:15 PM 0
Share

Thanks, now I just have to fix the gravity problem...

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 ;
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image POLYGAMe · Jul 01, 2012 at 04:19 AM 0
Share

That's a better answer :P

avatar image jasoncosma · Jul 01, 2012 at 11:14 PM 0
Share

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.

avatar image hathol · Jul 02, 2012 at 11:13 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

move position of child object on update - increment too small? 1 Answer

Network Tutorial Interpolation - TPS Auth 0 Answers

How To Move GameObjects In Fixed Path? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Position of a GameObject 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges