Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Peri_95 · Dec 23, 2017 at 02:44 AM · transform.position

Copying target's XY position to object's XY position

Hello community,

So, as the title say, I want to copy the position from one target object to the specific object where I'll put the script in. It's in 2D so I don't need the Z Axis, and I must not copy it because that will be the Z Axis of the camera over 2D area, that only works if it's moved back (in the Z Axis) from the background. The target will be the player where Z=0. Anyways, it looks like this:

 public GameObject target;

 private Vector2 xy;
 private Vector2 xyTarget;

 void Start () {
     xy.x = transform.position.x;
     xy.y = transform.position.y;

     xyTarget.x = target.transform.position.x;
     xyTarget.y = target.transform.position.y;
 } 


 void LateUpdate () {
     xy.x = xyTarget.x;
     xy.y = xyTarget.y;
 }

I assigned the target in the Inspector of course and the script has no compiler errors, but it doesn't work. I can't figure out what's the problem and would like to fix this script. It would mean a lot to me to learn what's the problem here.

P.S. Yeah, I am pretty much a beginner :D

Comment
Add comment
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

2 Replies

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

Answer by Peri_95 · Dec 23, 2017 at 08:02 PM

That way I would copy the whole position (Z Axis too, which I don't want). Besides that, in LateUpdate it gives an error by writing it like this, because I need to store target.transform.position for example as variable.

Anyways, you gave me an idea to try something else and I fixed my code by writing it like this:

   [SerializeField] 
 GameObject target;

 private Vector3 xy;
 private Vector2 xyTarget;

 void Start () {
     xy.x = transform.position.x;
     xy.y = transform.position.y;
     xy.z = transform.position.z;
 } 

 void LateUpdate () {
     xyTarget.x = target.transform.position.x;
     xyTarget.y = target.transform.position.y;
     xy.x = xyTarget.x;
     xy.y = xyTarget.y;

     transform.position = xy;
 }

So yeah. I had to put the part where I make (AND COPY) the Target position to the position of the object in the LateUpdate, because it has to be done every frame. In my case it didn't work because it just copied variable to variable. I didn't change anything else beyond that.

Thank you very much

Comment
Add comment · 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
1

Answer by Matchewman023 · Dec 23, 2017 at 02:49 AM

Your not applying the position. In the late update try doing:

 target.transform.position = transform.position;

this should set your targets position to the object the script's on position.

you can do it vice versa if that's what your trying to do

Comment
Add comment · 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

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

73 People are following this question.

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

Related Questions

Movement script for camera relative to where it looks 0 Answers

Why cant any operations be performed in camera when loaded from the previous scene? 0 Answers

Getting stuck in the ground while teleporting 0 Answers

How do I update the position of different prefabs to the current position? 0 Answers

transform.position any suggestions? 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