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

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 :frowning: will keep researching to see if i can get it but thanks in advance.

if (Input.GetKeyDown ("f")) {
            Debug.Log ("flash key pressed" + Time.time);
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast (ray, out hit)) {
                Vector3 mousePos = hit.point;
                Debug.Log ("x.pos" + mousePos.x);
                Debug.Log ("y.pos" + mousePos.y);
                Debug.Log ("z.pos" + mousePos.z);
                transform.LookAt (mousePos, Vector3.forward);
                transform.position = new Vector3(mousePos.x, mousePos.y, mousePos.z);
            }
            transform.position = transform.forward;
        }
//transform.position = new Vector3(mousePos.x, mousePos.y, mousePos.z);
// Simpler to write:
transform.position = mousePos;

// You were overwriting the mouse position with this for some reason?
//transform.position = transform.forward;

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?

Are there any other scripts attached to the player?

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

Send me the PlayerMovement script. It overwrites the position assigned by ur mouse click script.

    if (Input.GetKeyDown ("f")) {
        Debug.Log ("flash key pressed" + Time.time);
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast (ray, out hit)) {
            Vector3 mousePos = hit.point;
            Debug.Log ("x.pos" + mousePos.x);
            Debug.Log ("y.pos" + mousePos.y);
            Debug.Log ("z.pos" + mousePos.z);
            transform.LookAt (mousePos, Vector3.forward);
            agent.SetDestination(mousePos);
            transform.position = mousePos;
        } else {
            transform.position = transform.forward;
        }
    }

This should prevent the agent to go back to the previous destination.

    if (Input.GetKeyDown ("f")) {
        Debug.Log ("flash key pressed" + Time.time);
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast (ray, out hit)) {
            Vector3 mousePos = hit.point;
            Debug.Log ("x.pos" + mousePos.x);
            Debug.Log ("y.pos" + mousePos.y);
            Debug.Log ("z.pos" + mousePos.z);
            transform.LookAt (mousePos, Vector3.forward);
            agent.SetDestination(mousePos);
            transform.position = mousePos;
        } else {
            transform.position = transform.forward;
        }
    }

try this lol.