"Sneaking" in a Grid Based Movement Game?
I have a 2D stealth game which, before switching to grid based movement, involved slowing the player's movement speed to 1f upon holding left shift to simulate a "sneaking" state. However, when I switched to grid based movement, the player will always move at regular walking speed despite holding shift or not when moving in between grid squares. I'm wondering if it's possible to have something which will slow the walk speed between squares while holding left shift, and if so, how to implement it?
EDIT: Here's the code in question
Looking at this I'm realizing I can probably change to Vector2 since it's a 2D game, but I don't believe that's the problem with regards to slowing down the movement speed. The first few lines in update() would originally slow down the player movement upon holding left shift but no longer works with the grid based movement. Below is an excerpt of how the movement is conducted, there's some other stuff involved in the file which handles collisions but I felt it was irrelevant to the problem I'm having here.
I'm hoping to have the player's transition from one tile to another slow to 1f upon holding shift as opposed to the usual 5f movement speed that it seems to default at.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Movement : MonoBehaviour
{
private float moveSpeed = 5f;
public Transform movePoint;
public Animator animator;
public Collider2D coll;
private float saveHoriz = 0f;
private float saveVert = 0f;
void Start() {
movePoint.parent = null;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftShift)) {
moveSpeed = 0.1f;
Debug.Log(moveSpeed);
}
else {
moveSpeed = 5f;
Debug.Log(moveSpeed);
}
transform.position = Vector3.MoveTowards(transform.position, movePoint.position, moveSpeed * Time.deltaTime);
if (Vector3.Distance(transform.position, movePoint.position) <= .05f) {
if (Input.GetAxisRaw("Horizontal") == 1f) {
movePoint.position += new Vector3(Input.GetAxisRaw("Horizontal") + .6f, 0f, 0f);
saveHoriz = -1.6f;
saveVert = 0f;
animator.SetFloat("Vertical", 0f);
animator.SetFloat("Horizontal", Input.GetAxisRaw("Horizontal"));
animator.SetFloat("Speed", movePoint.position.sqrMagnitude);
}
else if (Input.GetAxisRaw("Horizontal") == -1f) {
movePoint.position += new Vector3(Input.GetAxisRaw("Horizontal") + -.6f, 0f, 0f);
saveHoriz = 1.6f;
saveVert = 0f;
animator.SetFloat("Vertical", 0f);
animator.SetFloat("Horizontal", Input.GetAxisRaw("Horizontal"));
animator.SetFloat("Speed", movePoint.position.sqrMagnitude);
}
else if (Input.GetAxisRaw("Vertical") == 1f) {
movePoint.position += new Vector3(0f, Input.GetAxisRaw("Vertical") + .6f, 0f);
saveHoriz = 0;
saveVert = -1.6f;
animator.SetFloat("Horizontal", 0f);
animator.SetFloat("Vertical", Input.GetAxisRaw("Vertical"));
animator.SetFloat("Speed", movePoint.position.sqrMagnitude);
}
else if (Input.GetAxisRaw("Vertical") == -1f) {
movePoint.position += new Vector3(0f, Input.GetAxisRaw("Vertical") + -.6f, 0f);
saveHoriz = 0;
saveVert = 1.6f;
animator.SetFloat("Horizontal", 0f);
animator.SetFloat("Vertical", Input.GetAxisRaw("Vertical"));
animator.SetFloat("Speed", movePoint.position.sqrMagnitude);
}
}
}
Answer by KhashayarJan · Dec 07, 2020 at 08:28 AM
There is always a way to do things. But the thing is we have no idea how you implemented your movement and grids and ... .
slow motion can be achieved by setting Time.timeScale to a value between 0 and 1. but that slows the whole scene.
give more information about how you're implementing your movement so we can help better
I've added some code to go along with my issue above, hopefully that will suffice
Your answer
![](https://koobas.hobune.stream/wayback/20220613021211im_/https://answers.unity.com/themes/thub/images/avi.jpg)