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

teleport player from clicking ui button

Discussion in 'Scripting' started by llJIMBOBll, Feb 20, 2015.

  1. llJIMBOBll

    llJIMBOBll

    Joined:
    Aug 23, 2014
    Posts:
    578
    hi im trying to make a fast travel system, im using unity ui and have a map with some buttons, the buttons are different locations, click on the button to travel to the position..
    i have this script, it works kinda, well in game i click on a button and my camera moves to the location but not my player and when i close the menu im back where i started...
    also not sure if it matters but time is stopped from pause/map menu and when menu closes time is normal again... thanx if u can help


    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

    public class FastTravel : MonoBehaviour {

    public Transform Location1;
    public Transform Location2;
    public Transform Location3;
    public Transform Location4;
    public Transform Location5;

    void Start (){

    }

    void Update (){

    }

    public void FT1() {
    transform.position = Location1.position;
    print(transform.position.x);
    }
    public void FT2() {
    transform.position = Location2.position;
    print(transform.position.x);
    }
    public void FT3() {
    transform.position = Location3.position;
    print(transform.position.x);
    }
    public void FT4() {
    transform.position = Location4.position;
    print(transform.position.x);
    }
    public void FT5() {
    transform.position = Location5.position;
    print(transform.position.x);
    }



    }
     
  2. iDontLikeHipsters

    iDontLikeHipsters

    Joined:
    Mar 24, 2014
    Posts:
    24
    Your camera moves but not your player? As in, it's a 3rd person game? What entity is this tied to?
     
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    Is this script on the player object or on some other object? From the described behavior it sounds like the script is on the camera and the camera perhaps has instructions to follow the player, so you're moving the camera and when you unpause the camera goes back to following the player like it's supposed to. If the script is indeed on the camera you'll also need to tell it to move the player object, such as player.transform.position.
     
  4. iDontLikeHipsters

    iDontLikeHipsters

    Joined:
    Mar 24, 2014
    Posts:
    24

    I think it would be easier to move the script to the Player object, considering I don't see much in here that relies on camera objects.
     
  5. llJIMBOBll

    llJIMBOBll

    Joined:
    Aug 23, 2014
    Posts:
    578
    hi its a first person view and the script in on the camera, the locations are empty gameobjects and assigned to the script.
    thanx for your help i will try player.transform tomorrow as icant right now, but another questions do i need to add a something to say what the player is? sorry im a noob but learning xd thank you
     
  6. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    If you were to leave the script on the camera, yes, you would need to either use a variable that lets you choose the player object in the inspector, or have the script find the object on start. Going the inspector route is more efficient since it's one less thing the script has to do.

    However, iDontLikeHipster is correct that in this case you would want to have the script on the player object instead of on the camera since there is nothing in the script (that we can see in your example at least) that has anything to do with the camera. If you go about it that way you don't need to worry about defining what the player object is, you can just use transform.position and it will be referring to the object the script is on.
     
  7. llJIMBOBll

    llJIMBOBll

    Joined:
    Aug 23, 2014
    Posts:
    578
    Wow thank you both!!! both your answers worked, but i went with putting the script on the fast travel gameobject so kept it all together and defined the player, so easy when u think about it xd
    here is the script as hate it when noone puts the working script when they find it out :D

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

    public class FastTravel : MonoBehaviour {

    public Transform Location1;
    public Transform Location2;
    public Transform Location3;
    public Transform Location4;
    public Transform Location5;
    public ac_FPParkour player;

    void Start (){

    }

    void Update (){

    }

    public void FT1() {
    player.transform.position = Location1.position;
    print(transform.position.x);
    }
    public void FT2() {
    player.transform.position = Location2.position;
    print(transform.position.x);
    }
    public void FT3() {
    player.transform.position = Location3.position;
    print(transform.position.x);
    }
    public void FT4() {
    player.transform.position = Location4.position;
    print(transform.position.x);
    }
    public void FT5() {
    player.transform.position = Location5.position;
    print(transform.position.x);
    }
    }