Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Help with new Player Position

Discussion in 'Scripting' started by Helmi172, Mar 11, 2021.

  1. Helmi172

    Helmi172

    Joined:
    Apr 7, 2020
    Posts:
    10
    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!
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    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.
     
    Helmi172 likes this.
  3. khanism

    khanism

    Joined:
    Dec 11, 2020
    Posts:
    9
    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:
    Code (CSharp):
    1.  
    2.   private void OnTriggerEnter(Collider other)
    3.    {
    4. if(other.gameobject.CompareTag("CertainArea"))
    5.        {
    6. //Change Player Position here
    7. }
    8.    }
    Method 2: Also You can use Vector3.Distance for calculating distance between two gameobjects like

    Code (CSharp):
    1. if(vector3.Distance(emptyGameobject.position,transform.position) <= 5){
    2. // Change Player Position here
    3. }
    For changing player position to a specific point over time you can use:
    1- https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
    2- https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

    For changing player position immediately to different position say targetPosition:

    Code (CSharp):
    1. transform.position = targetPosition.position
    Hope this helps
     
    Helmi172 likes this.
  4. Helmi172

    Helmi172

    Joined:
    Apr 7, 2020
    Posts:
    10
    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...



    }
    Code (CSharp):
    1.     public GameObject targetPosition;
    2.     public GameObject player;
    3.     private void OnTriggerEnter(UnityEngine.Collider other)
    4.     {
    5.         if (other.gameObject.CompareTag("are you seen")) ;
    6.         {
    7.             player.transform.position = targetPosition.transform.position;
    8.         }
    9.     }
    10.  
    11.  
    12.  
    13. }
     
  5. DaRealXDev

    DaRealXDev

    Joined:
    Feb 5, 2021
    Posts:
    13
    What object is that script on?
     
  6. Helmi172

    Helmi172

    Joined:
    Apr 7, 2020
    Posts:
    10
    on the fps controller