- Home /
Vector 3 moves object to wrong location?
I tried to make an object move to a position using a Vector 3. According to the Unity Script Reference for Vector 3, saying Vector3(0,1,0) should cause the object to move up. Instead, my object is moving to what seems to be a random point on my map (random in that I don't know why the object is moving over there). I even thought that maybe I needed to do Vector3(object coordinate, coordinate I want to go to, object coordinate) but that just shot the object out of the map and into the middle of nowhere. Maybe I'm misunderstanding how to use Vector3 properly?
So here is my code:
var coins : int;
function Awake ()
{
coins=1;
}
function Update () {
}
function OnTriggerEnter(other : Collider)
{
if (other.tag == "coin")
{
Destroy(other.gameObject);
coins--;
Debug.Log(coins);
}
if (other.tag == "Finish")
{
if (coins == 0)
{
Debug.Log("working");
var obj = (GameObject.FindGameObjectWithTag("Door"));
{
if(obj.name == "Door");
obj.transform.position = Vector3(0,1,0);
}
}
}
}
Answer by kyoujiru33 · Apr 27, 2014 at 12:51 AM
I figured out what the problem was. Apparently if you place an object in an empty that doesn't have the same coordinates as the object, then using Vector3 movement causes the object to go to a location based on the location of the object and the empty. I just removed the object from the empty and now the Vector3 is doing its job. Unity is so confusing sometimes...
In Inspector the values of Transform are Local, if the object is a child. Setting these to 0,0,0 will place the object at the centre of its parent.
Oh! Thank you, I didn't know that. I will keep that in $$anonymous$$d.
Answer by getyour411 · Apr 26, 2014 at 06:15 PM
Setting transform.position to a Vector3 like that is not a 'move' (magnitude/direction) per se, it's like a miniteleport. If you want to move (0,1,0) you need to use a move function; Google "Unity move object", look at Translate, rigidbody, CharacterController, etc.
I know that it teleports, it's just not teleporting up to the position I want it at. It's teleporting to a point across the map.