Hey Unity.
I just want to know how to move an object with scripts. Is it something about vector3? Can you show a script that if you press “q” a cube tagged “thebox” moves?
-sorry for bad english-
and for your help; thanks.
there are a number of ways to “move” something in Unity
you need to have a reference to the GameObject that has the tag.
GameObject cubeTaggedWithTheBox = ???
if(Input.GetKeyDown(KeyCode.Q))
{
cubeTaggedWithTheBox.transform.position = new Vector3 (player.transform.position);//1
cubeTaggedWithTheBox.Translate(Vector3.forward * Time.deltaTime);//2
cubeTaggedWithTheBox.GetComponent().ApplyForce(new Vector3(2.0f,0,0);//3
cubeTaggedWithTheBox.transform.RotateAround(…);//4
…
}
if the script is attached to the gameobject itself which will move, then cubeTaggedWithTheBox is just gameObject or “this”
Ex: this.transform.position…
or gameObject.transform.position…
or just transform.position.…
@ArachnidAnimal
Do you mean:
GameObject.cubeTaggedWithTheBox = “a” or a
if(Input.GetKeyDown(KeyCode.Q))
{
a.transform.position = new Vector3 (player.transform.position);//1
a.Translate(Vector3.forward * Time.deltaTime);//2
a.GetComponent().ApplyForce(new Vector3(2.0f,0,0);//3
a.transform.RotateAround(…);//4
…
}
I mean the latter part where you have just a, not “a”.
You cant use “a” because that is a string, not a gameObject.
Also, you cant use “GameObject__.__cubeTaggedWithTheBox” because the GameObject class doesn’t know what cubeTaggedWithTheBox is. you need to set a reference to the known game object that it is you are moving .
if you have a game object in your scene, just add your script to the game object that you want to move via the inspector panel, then you can just use “this.transform…” .
if your script is attached else where, you can use this:
This can cause problems if you have multiple game Objects with the same tag.
The best approach is to use the inspector panel, drag the gameObject from the hierarchy to the cubeTaggedWithTheBox variable in the script. see screen shot