Search Unity

Question Teleport Ability

Discussion in 'Getting Started' started by Kurigara, Dec 21, 2022.

  1. Kurigara

    Kurigara

    Joined:
    Dec 21, 2022
    Posts:
    2
    well i tried, and I'm sorry...I'm just dumb

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TeleporterAbility : MonoBehaviour
    6. {
    7.     // The range of the teleporter ability
    8.     public float range;
    9.  
    10.     // The cooldown of the teleporter ability
    11.     public float cooldown;
    12.  
    13.     // Private variables to keep track of elapsed time and whether the ability is on cooldown
    14.     private float elapsedTime;
    15.     private bool onCooldown;
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         // Check if the "r" key is pressed and the ability is not on cooldown
    21.         if (Input.GetKeyDown("r") && !onCooldown)
    22.         {
    23.             // Get the mouse position in screen coordinates
    24.             Vector3 mousePosition = Input.mousePosition;
    25.  
    26.             // Convert the mouse position to world coordinates
    27.             Vector3 worldPosition = Camera.main.ScreenToWorldPoint(mousePosition);
    28.  
    29.             // Calculate the distance between the player and the mouse position
    30.             float distance = Vector3.Distance(transform.position, worldPosition);
    31.  
    32.             // Check if the distance is within the range of the ability
    33.             if (distance <= range)
    34.             {
    35.                 // Teleport the player to the mouse position
    36.                 transform.position = worldPosition;
    37.  
    38.                 // Set the elapsed time and put the ability on cooldown
    39.                 elapsedTime = 0;
    40.                 onCooldown = true;
    41.             }
    42.         }
    43.  
    44.         // Increment the elapsed time
    45.         elapsedTime += Time.deltaTime;
    46.  
    47.         // Check if the elapsed time exceeds the cooldown time
    48.         if (elapsedTime >= cooldown)
    49.         {
    50.             // Set the ability to be off cooldown
    51.             onCooldown = false;
    52.         }
    53.     }
    54.  
    55.     void OnDrawGizmosSelected()
    56.     {
    57.         // Draw a wire sphere at the player's position with a radius equal to the range variable
    58.         Gizmos.DrawWireSphere(transform.position, range);
    59.     }
    60. }

    what should this do?
    when the keyboard "r" is pressed and point the mouse in the limited range, the player should teleport there and starts the cooldown.

    what this do?
    shows the gizmo
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    You are positioning the mouse incorrectly in 3D. The only effect you get is that you teleport the object to the center of the camera (like a cardboard over head). This video explains the rest.
     
    Last edited: Dec 22, 2022