So I'm want to move one game object (my player) to the position of another game object, on keyDown. I'm just not sure how to reference the position of the object which is where I want my player to move(or teleport) to.

I know a couple different was to move my player, but I don't know what variable or statement I would use to reference the other objects position.

(Also the thing I'm teleporting to is not moving). I really appreciate any help you can give me.

public class GetInCar : MonoBehaviour {
    public GameObject player;
    public GameObject car;
    //public Camera cam1;

    void OnMouseDown () {
        if (Input.GetKey (KeyCode.Mouse0)) {
            player.transform.rotation = car.transform.rotation;
            GameObject.Find ("Player").GetComponent<CharacterController> ().enabled = false;

            //cam1.enabled = true;
  • 1
    \$\begingroup\$ Does your player script have a public variable referencing the Transform of the GameObject you want to move to? Or do you need to search the scene for it using some criteria (like name, tags, attached scripts, physics checks, etc.)? \$\endgroup\$ – DMGregory Apr 17 '17 at 17:49
  • \$\begingroup\$ No because that's what I don't know how to reference. I have no clue how to assign a variable to another objects transform position. \$\endgroup\$ – nova nanite Apr 17 '17 at 18:37
  • 1
    \$\begingroup\$ Tell us a bit more about what this other object is and its relationship to the player. Is it something you can identify at editing time "oh, this is the object the player needs to move to. I'll just drag it into my player script..." or is it something that's created dynamically at runtime? If the latter, how is it identified? What's the distinguishing characteristic of this object that we can use to choose it from among all the other objects in your scene? \$\endgroup\$ – DMGregory Apr 17 '17 at 18:40
  • \$\begingroup\$ It's just a script that I applied to a block and I would just simply like to drag my game objects into the editor. \$\endgroup\$ – nova nanite Apr 17 '17 at 20:28
  • 1
    \$\begingroup\$ is player.transform.position = car.transform.position; not sufficient? Keep in mind that in your current code, both player and car are undefined. If they're defined in the inspector, then you don't need GameObject.Find("Player") as you already have it. If not, then you should assign the result of this find to player. \$\endgroup\$ – Draco18s no longer trusts SE Apr 17 '17 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.