Search Unity

Teleporting player to mouse position(last bit isn't working)

Discussion in 'Scripting' started by Gasric, May 17, 2016.

  1. Gasric

    Gasric

    Joined:
    Sep 8, 2013
    Posts:
    66
    Hi All,

    I have what i think is correct for teleporting a player to a mouse position.
    I can ray cast fine and turn and face the direction i want to teleport but i can't seem to get my head round the last part by teleporting to the location of the mouse.

    seems to teleport back to 0,0,0 cords :( will keep researching to see if i can get it but thanks in advance.

    Code (CSharp):
    1. if (Input.GetKeyDown ("f")) {
    2.             Debug.Log ("flash key pressed" + Time.time);
    3.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    4.             RaycastHit hit;
    5.             if (Physics.Raycast (ray, out hit)) {
    6.                 Vector3 mousePos = hit.point;
    7.                 Debug.Log ("x.pos" + mousePos.x);
    8.                 Debug.Log ("y.pos" + mousePos.y);
    9.                 Debug.Log ("z.pos" + mousePos.z);
    10.                 transform.LookAt (mousePos, Vector3.forward);
    11.                 transform.position = new Vector3(mousePos.x, mousePos.y, mousePos.z);
    12.             }
    13.             transform.position = transform.forward;
    14.         }
     
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Code (CSharp):
    1. //transform.position = new Vector3(mousePos.x, mousePos.y, mousePos.z);
    2. // Simpler to write:
    3. transform.position = mousePos;
    4.  
    5. // You were overwriting the mouse position with this for some reason?
    6. //transform.position = transform.forward;
     
  3. Gasric

    Gasric

    Joined:
    Sep 8, 2013
    Posts:
    66
    Thanks i noticed and i have a new issue which has just made me puddled.

    When i teleport to the location the player walks back to the original teleport position.

    How can i stop this please?
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Are there any other scripts attached to the player?
     
  5. Gasric

    Gasric

    Joined:
    Sep 8, 2013
    Posts:
    66
    Hi magiichan

    PlayerMovement script

    I have worked out a way to do it but its a little robust, i disabled the navmeshagent which did the trick but I'm sure there is a smoother way of doing this?

    Thanks
     
  6. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Send me the PlayerMovement script. It overwrites the position assigned by ur mouse click script.
     
  7. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Code (CSharp):
    1.     if (Input.GetKeyDown ("f")) {
    2.         Debug.Log ("flash key pressed" + Time.time);
    3.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    4.         RaycastHit hit;
    5.         if (Physics.Raycast (ray, out hit)) {
    6.             Vector3 mousePos = hit.point;
    7.             Debug.Log ("x.pos" + mousePos.x);
    8.             Debug.Log ("y.pos" + mousePos.y);
    9.             Debug.Log ("z.pos" + mousePos.z);
    10.             transform.LookAt (mousePos, Vector3.forward);
    11.             agent.SetDestination(mousePos);
    12.             transform.position = mousePos;
    13.         } else {
    14.             transform.position = transform.forward;
    15.         }
    16.     }
    This should prevent the agent to go back to the previous destination.
     
    Last edited: May 18, 2016
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Code (CSharp):
    1.     if (Input.GetKeyDown ("f")) {
    2.         Debug.Log ("flash key pressed" + Time.time);
    3.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    4.         RaycastHit hit;
    5.         if (Physics.Raycast (ray, out hit)) {
    6.             Vector3 mousePos = hit.point;
    7.             Debug.Log ("x.pos" + mousePos.x);
    8.             Debug.Log ("y.pos" + mousePos.y);
    9.             Debug.Log ("z.pos" + mousePos.z);
    10.             transform.LookAt (mousePos, Vector3.forward);
    11.             agent.SetDestination(mousePos);
    12.             transform.position = mousePos;
    13.         } else {
    14.             transform.position = transform.forward;
    15.         }
    16.     }
    try this lol.