Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Character teleportation

Discussion in 'Scripting' started by zeropiunebarre, Aug 16, 2019.

  1. zeropiunebarre

    zeropiunebarre

    Joined:
    Aug 14, 2019
    Posts:
    10
    Hello, i am new to coding and i was wondering if someone could help me figure out how to implement a teleport input to my character. What i mean by that is like when u press a key it loads a teleport animation then it teleports you once you release the key based on how long you hold it. I was thinking of doing a script thats like a jump but in the X axis so that it acts as a teleport, but i am unsure on how to modify the player controller that i bought from the asset store.

    Edit: the game is a 2d platformer
    Thanks alot!
    Ps: English is not my main language so sorry about the bad spelling and grammar.
     
    Last edited: Aug 16, 2019
  2. gononono64

    gononono64

    Joined:
    Mar 1, 2015
    Posts:
    13
    Is this 2d or 3d?
     
  3. zeropiunebarre

    zeropiunebarre

    Joined:
    Aug 14, 2019
    Posts:
    10
    It is a 2d platformer
     
  4. zeropiunebarre

    zeropiunebarre

    Joined:
    Aug 14, 2019
    Posts:
    10
  5. Kwel

    Kwel

    Joined:
    Jun 9, 2014
    Posts:
    80
    I guess it depends on the player controller you are referring to... Could you share a link to it? Maybe someone here knows it!

    But basically:

    For the inputs (press ans release), check the Input class documentation: Input
    Looks like GetButtonDown (or KeyDown) and GetButtonUp (or KeyUp) should help there.

    Then for the teleportation itself, changing the "transform.position" of your character could do?
     
  6. gononono64

    gononono64

    Joined:
    Mar 1, 2015
    Posts:
    13
    Dont worry im comin in with the big boi spoon.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterTeleporter : MonoBehaviour
    6. {
    7.  
    8.     public GameObject teleportLocation;
    9.  
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         if (Input.GetButton("Fire1"))
    15.         {
    16.             OnButtonPress();
    17.         }
    18.     }
    19.  
    20.     public void OnButtonPress()
    21.     {
    22.         Vector3 newPos = new Vector3(teleportLocation.transform.position.x, teleportLocation.transform.position.y, this.transform.position.z);
    23.         transform.position = newPos;
    24.     }
    25. }
    26.  
    Place it on your character.
    Create Game Object to use as location.
    Feel free to replace "Fire1" with any button listed in Edit -> Project Settings -> Input Manager.
    Any more and ill consider charging... jk;)
     
    Kwel likes this.
  7. zeropiunebarre

    zeropiunebarre

    Joined:
    Aug 14, 2019
    Posts:
    10
  8. gononono64

    gononono64

    Joined:
    Mar 1, 2015
    Posts:
    13
    Pulls out big boi ladle:

    Again if you are serious about game development i would look into c# unity tutorials

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class CharacterTeleporter : MonoBehaviour
    6. {
    7.     public string button = "Fire1";
    8.     public float distanceMod = 1f; //distance scaler
    9.     public Vector3 direction = Vector3.right; // the x,y,z should really only be set to a value between 0 and 1 but if set higher techically does the same thing as the distance mod
    10.     [SerializeField] // just here so we can show the private variable below in the inspector
    11.     private float timeHeld = 0f;
    12.     private bool heldDown = false; // a variable to know when our button is being held down
    13.    
    14.  
    15.     void Update()
    16.     {
    17.         if (Input.GetButtonDown(button)) //this only fires the moment the button is pressed
    18.         {
    19.             heldDown = true; // which is why we have to use this var
    20.         }
    21.    
    22.         if (heldDown)
    23.         {
    24.             OnButtonPress();
    25.         }
    26.         if (Input.GetButtonUp(button)) // for this function we dont need anything more than the moment the button gets released
    27.         {
    28.             OnButtonRelease();
    29.             heldDown = false;
    30.         }
    31.     }
    32.  
    33.     public void OnButtonPress() // technically this could be placed in the update function along with OnButton release but its just habbit
    34.     {
    35.         timeHeld += Time.deltaTime;// adding the amount of time passing per update tick and adding to timeHeld variable  
    36.     }
    37.  
    38.     public void OnButtonRelease()
    39.     {
    40.         Vector3 newPos = direction *distanceMod * timeHeld; //this creates our new position
    41.         this.transform.position += newPos; //adds the new position onto current position
    42.         timeHeld = 0f; //resets our time held
    43.     }
    44. }
    Note: Please read green text about variables
     
    Kwel likes this.