- Home /
Move 'Player' To new position.
So im making a large BattleField style RTS and i have gotten my tank entering system working but i need a way so when you get out it places the 'player' which is a fps controller prefab next to the tank.
Thanks in advance.
Answer by · Aug 20, 2010 at 06:30 AM
As 0V3RWR173D said, it's a bit difficult to give you an exact answer without knowing how your system is setup.
var player : GameObject;
function OnStart () { player = GameObject.Find("Player"); }
function Update () { if (Input.GetKeyDown("e") /&& player is in tank/) // commented out for your code to replace { tank = GameObject.Find("Tank"); // finds the GameObject named if ( tank ) { player.position = (tank.position + (2,0,0)); player.rotation = tank.rotation; // make the player visible here } } }
This setup will test when 'e' is pressed, find the GameObject named 'Tank' and teleport the Player prefab 2 units to the right of its current position with the same rotation. You'll need to specify the Player prefab on the GameObject you attach this script to. You'll also want to update the "/&& player is in tank/" with a check that works with your code to determine whether the player is inside the tank (or you'll get teleported every time it's pressed). One obvious limitation is that if the tank is on a slope, the player could be set to a position underneath/inside the terrain. Games typically deal with this issue through a set of raycast checks before allowing the Player to exit the vehicle.
Ok, The 'Player' is still in play, just invisible. I just want to change the position of it from a script not attached to the 'player'.
I've updated the script to teleport the player rather than spawn it. You'll need to change the "player = GameObject.Find("Player");" line to match the name of your player character object (or use FindWithTag) and make the player visible again (where commented) by doing the opposite of however you're making them invisible. Let me know if you have any troubles.
Answer by 0V3RWR173D · Aug 20, 2010 at 05:17 AM
You can use javascript to change the location. If you don't know how to script then it will be difficult. Depending on what you want to do, it may be difficult if you are new to unity. You can use:
function Update () {
if (Input.GetKeyDown("e")) {
transform.position = Vector3(0, 0, 0);
}
}
The Update means that it will check every frame to see if the "if" statement is true. After the if statement "(Input.GetKeyDown("e"))" means that it will check if you pressed the "e" key on your keyboard. "transform.position" is referring to the position of the object that the script is attached to. "= Vector3(0, 0, 0)" means that you are changing the position of the object to 0 on each axis.
I don't know your current situation so I cannot help you any more for the moment. You can also have an object broadcast a message to your player when something happens (ex. the player collides with a certain object or leaves a certain area).
Your answer
![](https://koobas.hobune.stream/wayback/20220613071651im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move character by X units on the left 1 Answer
Vector3 and Quaternion issues. 2 Answers
Vector3.lerp doesn't work 1 Answer
Move empty object random 1 Answer