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 ObeliskEntertainment · May 25, 2014 at 06:05 AM · instantiateraycasttransformposition

Help with moving instantiated object's transform

Hello guys, this is an edited question. I reread what I originally posted and decided that I need to explain what I need help with better. Okay let me piece this together carefully.

//The black squares are instantiated at start at random from the Top row, to the row just above the Yellow.

//I need the blocks to move down EXACTLY -1.25f on the Y axis. In other words the block will move down 1 space. (They all need to move together).

//Only the black block closest to the yellow will execute this command, the others should not do this until they have reached the y axis of -3.75f.

//The gameobject's name is "BlackTileClone" with a tag of "BlackTile"

//I send a raycast to mouse position with a if statement of "touchcount > 0"

//I've tested various methods like FindGameObjectWithTag and GameObject.name and my results weren't so good. I tried using a foreach loop and it appears that my -1.25 was multiplied by the number of instantiated blocks. (numBlackBlocks * -1.25).

//I know the answer is right in front of my. I feel as though I've stressed myself out to the point that everything is complicated. >.<

Here is the picture of my game. Click to View game grid

I have added some code that I feel is very close and precise. I put a lot of effort into commenting out every decision I made.

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {
     private bool canexecute;
     private GameObject[] grabBlackClonesArray;
  
     //We created our bool to determine if the cube is at -3.75f
     //We also created the GameObject Array
     
     
     
     void Start()
     {
         //We set the array to have slots 0,1,2,3 (total of 4)
         grabBlackClonesArray = new GameObject[3];
 
         //We debug to see if the position 0 is not null
         Debug.Log (grabBlackClonesArray[0]);
     }
     
     
     
     
     
     void Update() {
 
         //We finally set grabBlackClonesArray and fill it up with all objects
         //with the tag "BlackTile"
 
         grabBlackClonesArray = GameObject.FindGameObjectsWithTag("BlackTile");
 
         //We check to see if it is true
         Debug.Log (grabBlackClonesArray[0]);
         //To make sure that the movement is only done once we put it in a loop
         //remember we are in the Update method..it's called every frame
         for(int i = 0; i < 1; i++) {
             //With this code the Black Block in slot [1] SHOULD only move EXACTLY 
             //-1.25f from it's CURRENT location. However this isn't true.
             //I get these numbers, far from being exact.
             //X:1.657822 Y:-0.6175802 Z:-13.79006
             //Also somewhere around here is were we would include our bool
             //to check if the block in [1] is in the -3.75 Y position, and if it is it can be executed then moved up top
             //in a random vector (Reusing resources)
             grabBlackClonesArray [1].transform.position = new Vector3 (transform.position.x, transform.position.y -1.25f , transform.position.z);
         }
     }
 }


It appears as though it's because my instantiated objects are placed using Random.Range. The blacktile that I change the vector value of ALWAYS stays in the same place with each game restart. It is no longer random like it should be.

blackgame.png (8.6 kB)
Comment
Add comment · Show 3
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 ObeliskEntertainment · May 25, 2014 at 11:00 PM 0
Share

You are my next best friend, I tested your code (grabBlackClonesArray [1].transform.position -= new Vector3 (0.0f, -1.25f, 0.0f);) and it worked perfectly. However, the - made it go up so I went to a positive. Now I just need all black blocks to move like this.

avatar image T27M · May 25, 2014 at 11:02 PM 0
Share

Ah, yes I had a double negative, glad it works. I've updated and converted it to an answer.

avatar image ObeliskEntertainment · May 25, 2014 at 11:05 PM 0
Share

By reading the code you gave me, I am seeing that when you put -= you are taking the current value and subtracting any value. So that is why X and Z are 0f. If I were to change them they would move as well. I feel really dumb...How could I forget the basics >.<.

1 Reply

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

Answer by T27M · May 25, 2014 at 10:53 PM

At line 16 in the code above

 grabBlackClonesArray = new GameObject[3];

This will declare an array of 3 elements (0,1,2). It is 0 index, but you still declare the size as the number you want i.e if you want 4 you do

 grabBlackClonesArray = new GameObject[4];

That will give you (0,1,2,3).

What happens if you replace this line

 grabBlackClonesArray [1].transform.position = new Vector3 (transform.position.x, transform.position.y -1.25f , transform.position.z);

with

 grabBlackClonesArray [1].transform.position -= new Vector3 (0.0f, 1.25f, 0.0f);
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

21 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

Related Questions

transform position and rotation of instantiated object 1 Answer

Getting transform position of last parent 2 Answers

Instantiate Bullet Hole position and rotation, Vector3 issues 1 Answer

I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers

Copy position from one object to another on one axis 0 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