Hey,
i need to change the player position as soon as he enters a certain area with a simple script, how do i do this best?
my player is already movin with a gameobject in case that matters…
thank in advance!
What part are you stuck on? This generally consists of a few components. Detect when the player enters such an area. Know for each area where the player should be moved to. Then, when you detect the player entering an area, get the correct position for that area and move the player there.
Method 1: Create a 3D object preferably cube disable it’s mesh renderer component, add box collider to it & check “IsTrigger” bool of box collider component. Also add some tag like “CertainArea”
Make sure your player has Rigidbody component attach to it.
and use:
private void OnTriggerEnter(Collider other)
{
if(other.gameobject.CompareTag("CertainArea"))
{
//Change Player Position here
}
}
Method 2: Also You can use Vector3.Distance for calculating distance between two gameobjects like
if(vector3.Distance(emptyGameobject.position,transform.position) <= 5){
// Change Player Position here
}
For changing player position to a specific point over time you can use:
1- Unity - Scripting API: Vector3.MoveTowards
2- Unity - Scripting API: Vector3.Lerp
For changing player position immediately to different position say targetPosition:
transform.position = targetPosition.position
Hope this helps
thanks a lot so far,
well thats how far i have come, when i cross the triggerbox the screen just flickers a little bit…
im using the fps from the unity standard assets and i think the fact that im already movin with my train somehow makes the script not working…
}
public GameObject targetPosition;
public GameObject player;
private void OnTriggerEnter(UnityEngine.Collider other)
{
if (other.gameObject.CompareTag("are you seen")) ;
{
player.transform.position = targetPosition.transform.position;
}
}
}
What object is that script on?
on the fps controller