How to stop transform position when condition meets in unity?

I need someone to help me with transform.position in Unity3d. I am trying to stop tranform.position when M key is pressed. I tried with the below mentioned code but it's still moving in same speed.

public class Scroll : MonoBehaviour { private void FixedUpdate() { if (PlayerController.isDead) return; if (Input.GetKey(KeyCode.M)) { transform.position += PlayerController.player.transform.forward * -20f * Time.deltaTime; } else { transform.position += PlayerController.player.transform.forward * -5f * Time.deltaTime; } if (PlayerController.currentPlatform == null) return; if (PlayerController.currentPlatform.tag == "stairsUp") this.transform.Translate(0, -0.06f, 0); if (PlayerController.currentPlatform.tag == "stairsDown") this.transform.Translate(0, 0.06f, 0); } } 

In normal condition transform.position is working but when I press the M key it should be slow down as much as it should look like its stopped now. I think Else condition is running perfectly.

0

2 Answers

Make sure you don't have any other interfering code and try this (only this in your update (FixedUpdate) methed to stop your transform from moving.

if (Input.GetKey(KeyCode.M)) return; 

You should be able to use Input.GetKey in FixedUpdate, however for Input.GetKeyDown / Input.GetKeyDown I would recommend using Update to make sure no events get lost.

If that does not work, try to add this to a cube (or something visual) in your scene. This will certainly move and the stop as soon as you press M.

using UnityEngine; public class MoveTest : MonoBehaviour { private void FixedUpdate() { if (Input.GetKey(KeyCode.M)) return; transform.position += Vector3.forward * -5f * Time.deltaTime; } } 

--- OLD ANSWER BEFORE UPDATED POST --

You should use GetKey instead

Returns true while the user holds down the key identified by name.

You are using GetKeyDown that only returns true during the frame you pressed the key resulting in slowdown only one frame ¬

Returns true during the frame the user starts pressing down the key identified by name.

3

I am trying to stop tranform.position when M key is pressed.

Currently you reach your if block exctly in one single frame namely the moment the key goes down the first time. Input.GetKeyDown

Returns true during the frame the user starts pressing down the key identified by the key KeyCode enum parameter.

For continues executions you would want to use Input.GetKey which

Returns true while the user holds down the key identified by the key KeyCode enum parameter.

if(Input.GetKey(KeyCode.M)) { // you will have to adjust the multiplicators transform.position += PlayerController.player.transform.forward * -0.00001f * Time.deltaTime; } else { transform.position += PlayerController.player.transform.forward * -0.1f * Time.deltaTime; } 

Btw if you really want to stop the movement as you say you would probably rather simply use

if(!Input.GetKey(KeyCode.M)) { transform.position += PlayerController.player.transform.forward * -0.1f * Time.deltaTime; } 

and do nothing while M stays pressed


Note that your code is frame-rate dependent and you always should use * Time.deltaTime for converting your values from Units / frame into Units / second

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like