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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Move Player to the position of X

Discussion in 'Scripting' started by Argonxx, Nov 15, 2020.

  1. Argonxx

    Argonxx

    Joined:
    Apr 16, 2017
    Posts:
    37
    Hi,
    I have a simple question. I have a player and a cube in 3D. The cube is a trigger collider.
    I want to change player position and rotation according to the cube position and rotation when he stays in this trigger.
    How to use transform.position of a player to do that?
    I found something like this, but I don't understand it:

    private void OnTriggerStay(Collider other)
    {
    if (Input.GetKey(KeyCode.R) && other.tag == "Cube")
    {
    Vector3 targetDir;
    targetDir = new Vector3(transform.position.x - character.transform.position.x, 0f, transform.position.z - character.transform.position.z);
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    This is sort of like finding an elephant's toenail and thinking it is a good indication of what an elephant looks like.

    Go work through some full tutorials on opening doors or opening chests in 3D. There are probably literally thousands of tutorials out there, and the concepts are pretty straightforward, although everybody will do it differently.

    Here are the basic concepts involved, which you can look for as you work on the tutorial(s):

    1. detect the player is presently in the correct location (triggers, areas, etc.)
    2. detect the player has commanded something (keyboard, button, input, whatever)
    3. doing the thing you want to have happen.

    Keep in mind the script is 10% of the problem. About 90% of the problem is setting up the scene and/or prefabs to actually function with the script. The tutorials will guide you through this.
     
  3. lubba64_unity

    lubba64_unity

    Joined:
    Sep 16, 2020
    Posts:
    9
  4. Argonxx

    Argonxx

    Joined:
    Apr 16, 2017
    Posts:
    37
    @Kurt-Dekker
    I want player to sit on the chair. There is only one tutorial on YT and it doesn't work. So I found another solution. I have already animation of getting sit and turning around. It all works but I need only to change position of a player to the position of a chair when the player stays in the trigger and I press the R key. So:

    Code (csharp):
    1.  
    2.  
    3. void Start()
    4.     {
    5.         anim = GetComponent<Animator>();
    6.         chair = GameObject.Find("Chair");
    7.     }
    8.  
    9. private void OnTriggerStay(Collider other)
    10.         {
    11.             if (Input.GetKey(KeyCode.R) && other.tag == "Chair")
    12.             {
    13.             anim.SetInteger("Round", 1);
    14.             anim.SetInteger("SitDown", 1);
    15.             anim.SetInteger("StandUp", 0);
    16.             Vector3 targetDir;
    17.             targetDir = new Vector3(transform.position.x - character.transform.position.x, 0f, transform.position.z - character.transform.position.z);
    18.          
    19.             Quaternion rot = Quaternion.LookRotation(targetDir);
    20.             character.transform.rotation = Quaternion.Slerp(character.transform.rotation, rot, 0.05f);
    21.             character.transform.Translate(Vector3.forward * 0.01f);
    22.             }
    23.         }
    24.  
    And in this code only animations works, but positioning of a player not. I need something about, when you press R and he is in the trigger box named 'Chair' take that chair position.