Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Boucer_Boy · May 03 at 08:02 PM · movementtargeting

I need help for code to distinguish the difference between two game objects with the same tag

My code right now can move one of the white blocks and allow it to stay between the walls, not get pushed out. Similar to the box/text movement in baba is you. What I want to do is have multiple boxes that follow this same movement. The boxes and player are being tracked by their Vector3 on the game by their tag. Is there any way that I can use the same tag, but it can allow for the boxes to move independently? The error right now is that it is only tracking one of the boxes and then when I move that box, the other block teleports to the location. Using transform.position for the movement and since the player was shifting to the side slightly the positions and rotations are frozen.

public class BlockPushing : MonoBehaviour { private Vector3 player; private Vector3 block; private Vector3 Top; private Vector3 Left; private Vector3 Right; private Vector3 Down; private Vector3 Wall; public float speed = 1.25f; private bool OnOff = true; // Update is called once per frame void Update() { player = GameObject.FindGameObjectWithTag("player").transform.position; block = GameObject.FindGameObjectWithTag("block").transform.position; Top = GameObject.FindGameObjectWithTag("Up").transform.position; Left = GameObject.FindGameObjectWithTag("Left").transform.position; Right = GameObject.FindGameObjectWithTag("Right").transform.position; Down = GameObject.FindGameObjectWithTag("Down").transform.position; Wall = GameObject.FindGameObjectWithTag("Wall").transform.position;

     //make the block move when pushed

     //the player.x == block.
     if (Input.GetKeyDown(KeyCode.G) && (player.x != block.x || player.y != block.y))
     {

         if (OnOff)
         {
             OnOff = false;
         }
         else
         {
             OnOff = true;
         }

     }
     if (player.x == block.x && block.y == player.y && OnOff)
     {
         
             //left
             if (Left.x + speed < block.x)
             {
                 if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
                 {
                     transform.position = new Vector3(block.x - speed, block.y, 1);
                 }
             }
             //right
             if (Right.x - speed > block.x)
             {
                 if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
                 {
                     transform.position = new Vector3(block.x + speed, block.y, 1);
                 }
             }
             //up 
             if (Top.y - 2* speed > block.y)
             {
                 if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
                 {
                     transform.position = new Vector3(block.x, block.y + speed, 1);
                 }
             }
             //down
             if (Down.y + 2 * speed < block.y)
             {
                 if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
                 {
                 
                     transform.position = new Vector3(block.x, block.y - speed, 1);
                 }
             }
         
     }
          

} }

game-preview.png (44.7 kB)
Comment
Add comment · Show 1
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 Boucer_Boy · May 04 at 02:04 PM 0
Share

public class BlockPushing : MonoBehaviour { private Vector3 player; private Vector3 block; private Vector3 Top; private Vector3 Left; private Vector3 Right; private Vector3 Down; private Vector3 Wall;

 public float speed = 1.25f;
 private bool OnOff = true;
 // Update is called once per frame
 void Update()
 {
     player = GameObject.FindGameObjectWithTag("player").transform.position;
     block = GameObject.FindGameObjectWithTag("block").transform.position;
     Top = GameObject.FindGameObjectWithTag("Up").transform.position;
     Left = GameObject.FindGameObjectWithTag("Left").transform.position;
     Right = GameObject.FindGameObjectWithTag("Right").transform.position;
     Down = GameObject.FindGameObjectWithTag("Down").transform.position;
     Wall = GameObject.FindGameObjectWithTag("Wall").transform.position;


     //make the block move when pushed

     //the player.x == block.
     if (Input.GetKeyDown(KeyCode.G) && (player.x != block.x || player.y != block.y))
     {

         if (OnOff)
         {
             OnOff = false;
         }
         else
         {
             OnOff = true;
         }

     }
     if (player.x == block.x && block.y == player.y && OnOff)
     {
         
             //left
             if (Left.x + speed < block.x)
             {
                 if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
                 {
                     transform.position = new Vector3(block.x - speed, block.y, 1);
                 }
             }
             //right
             if (Right.x - speed > block.x)
             {
                 if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
                 {
                     transform.position = new Vector3(block.x + speed, block.y, 1);
                 }
             }
             //up 
             if (Top.y - 2* speed > block.y)
             {
                 if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
                 {
                     transform.position = new Vector3(block.x, block.y + speed, 1);
                 }
             }
             //down
             if (Down.y + 2 * speed < block.y)
             {
                 if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
                 {
                 
                     transform.position = new Vector3(block.x, block.y - speed, 1);
                 }
             }
         
     }
          

} }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rh_galaxy · May 03 at 08:08 PM

Without having read the code at depth, maybe you can set the name of the object differently when you create/instantiate them as well as the tag. Also GameObject.FindGameObjectWithTag() is not something you should run every frame (in Update()) because it is costly, do it once in Start() and keep the info so you can use it in Update().

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
0

Answer by Boucer_Boy · May 12 at 07:40 PM

I've solved my problem so I'll share it here in case it might help someone.

public float[] numberblocks; public int NumberOfBlocks; //change this in the unity UI

Awake(){ numberblocks = new float[NumberOfBlocks]; }

for (int x = 1; x <= NumberOfBlocks; x++) {

     numberblocks[x-1] = "" + x;
         block = GameObject.FindGameObjectWithTag("block" + x).transform.position;

// just make sure the tags increase numarically

}

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

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

Lock onto a target 1 Answer

Accessing a variable from another script on the same object? C# 1 Answer

Character doesn't stop and sits on top of target. 1 Answer

Move Gameobject With Mouse 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 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