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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Slow Time and Teleport Player

Discussion in 'Scripting' started by joeads_unity, Mar 4, 2019.

  1. joeads_unity

    joeads_unity

    Joined:
    Mar 4, 2019
    Posts:
    1
    Hello, I'm very new to Unity and don't know where to start! I have a FPS game set up, and I want to have the player be able to press a button (right now it's right click, but I may change it to E) that will slow the game down, and allow them to choose a place to teleport to. It'll show an outline/transparent version of the player to show where you can move the mouse to place the player. When the button is clicked again, or a few seconds have passed, the game will go back to normal speed and the player will teleport to wherever they chose.

    Right now, all I have is a slow time script, but it only works when the button is held down, rather than when
    clicked.

    Any help would be greatly appreciated!

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class SlowTime : MonoBehaviour
    {
    // Toggles the time scale between 1 and 0.7
    // whenever the user hits the Fire1 button.
    void Update()
    {
    if (Input.GetMouseButton(1))
    {
    Time.timeScale = 0.5f;
    }
    else
    {
    Time.timeScale = 1.0f;
    Time.fixedDeltaTime = 0.02f * Time.timeScale;
    }
     
  2. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    I would use a bool instead.

    So when mouse button is clicked you set the bool variable slowtime , or whatever you want to call it, to either true or false.
    Then use that on your if statement for the time scale.
     
  3. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Yep use a bool or try a timer which counts up deltatime until your desired time
     
  4. dontdiedevelop

    dontdiedevelop

    Joined:
    Sep 18, 2018
    Posts:
    68
    Code (CSharp):
    1. float lastPressTime;
    2. void Update()
    3.     {
    4.         if (Input.GetMouseButton(1))
    5.         {
    6.             Time.timeScale = 0.5f;
    7.             lastPressTime = Time.time;
    8.         }
    9.         else if(Time.timeScale != 1 && Time.time >= lastPressTime + 5f)
    10.         {
    11.             Time.timeScale = 1;
    12.         }
    13. }