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 Tyler 2 · Feb 05, 2011 at 05:15 AM · positionobjectgame

Object position question...

I am trying to set up a system in my game that allows the position of two objects that are surrounding the object that has the script attached to it that are within a certain range (In this case, I want one object on the left and one on the right, so I would check the positions along the x axis). Then, when a certain key is pressed, I want to switch the positions of the two objects (so the one that was on the left will now be on the right and vice versa). If it helps here is a video showing what I want (the cursor that switches the blocks in this game, just imagine there is a game object in the center of the cursor, between two blocks).

My idea was to: 1 check the scene for the type of object I want to switch(I would do this every second or so, as to not slow down the game that much), in this case, that means objects with the tag "block". 2 Get the positions of two objects specific that I want. 3 Then, when the player clicks down, invoke the function that switches the objects.

I am a little confused on exactly what to do. How can I get the positions of the objects and how can I isolate the two that are within the range from object (the way my game is laid out, there will only be two within that range at all times, its a puzzle game and the objects laid out in a grid of tiles)?

Here is code I have so far. (A and B are the two objects on the left and the right

private var A : GameObject; private var B : GameObject;

function Start () { InvokeRepeating("Find",0,1); } function Find () { /SOMETHING/ } function Update () { if( Input.GetButtonDown( "Fire1" )){{if(GameObject.position - B).x <= 1 && (GameObject.position - A).x >=-1 && <=0){Invoke("Switch",0) } } } }

function Switch () { B.transform.position = A.transform.position; A.transform.position = B.transform.position; }

I know that was alot, but thank you.

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

0 Replies

· Add your reply
  • Sort: 

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

No one has followed this question yet.

Related Questions

Returning position of game object in c# 2 Answers

Game object position. 1 Answer

how to scale an object in order to fix the screen dimensions 3 Answers

How to render object in front of other object no matter position? 1 Answer

Object changes its positions for no reason 1 Answer


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