Reputation: 62
I am making a 2d shooter game. I have a Gameobject that is "Gun" when the Player Gameobject touches the gun it goes to the position of the player. However, I then would like to move the gun from that position on the player sprite to the sprits hand. I thought that the easiest method to do this would be to add a Vector on to the starting vector, to change the coordinates of the gun. However I am not sure how to add the vectors, this is what I have tried but it comes up with the error: 'Vector3' cannot be used as a method.
Vector3 coords = GameObject.FindGameObjectWithTag("Gun").transform.position = GameObject.FindGameObjectWithTag("Player").transform.position;
Vector3 newcoords = coords + Vector3(1, 1, 0)
Upvotes: 1
Views: 13789
Reputation: 1775
The easiest way to do it is (given that your player is properly defined as a tree structure, head, torso, hand etc), is to assign to the weapon's the local position of the hand, or even better to put the weapon as a child node of your hand. Therefore when you'll make any changes within hand, transformation, rotation etc your weapon will react accordingly. Just find "hand" within your player's structure and then assign to "weapon.parent = hand" (that's pseudocode of course, but you know the gist).
Of course if your weapon had a world's position already assigned, after making it child of player's hand you will have to set the weapon.position again, relative to position of the hand (you can set it up to Vector3.zero and see what you'll get as a result)
Upvotes: 1
Reputation: 306
You have to create instance of the Vector3:
Vector3 newcoords = coords + new Vector3(1, 1, 0)
Upvotes: 2