How to make player move forward once, snap to gameobject and then return slowly and smoothly to its default starting position?
C# Only please This is also a 3D project semi-top down view. Hello, I was wondering how I can make this white cube (the player) snap to the yellow cube in the direction pressed and then return back to the starting position that the white cube (the player) is in smoothly.(Won't a slerp come into play when trying to make the cube return smoothly to the starting position?) My script is not correct and I'm trying to understand what is wrong.
I just want the cube to move to one of the yellow squares in the direction pressed [ w key moves the cube forward, a key moves the cube left towards the yellow cube, etc] and then snaps to the yellow square for 1 second and returns back to the middle / starting position.
using UnityEngine;
using System.Collections;
public class MoveCube : MonoBehaviour {
public float moveSpeed = 1f;
private Vector3 myPosition;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.W))
transform.position += new Vector3
(moveSpeed * Time.deltaTime, 0.0f, 1f);
if (Input.GetKey(KeyCode.A))
transform.position += new Vector3
(moveSpeed * Time.deltaTime, -1f, 0.0f);
if (Input.GetKey(KeyCode.S))
transform.position += new Vector3
(moveSpeed * Time.deltaTime, 1f, 0.0f);
if (Input.GetKey(KeyCode.D))
transform.position += new Vector3
(moveSpeed * Time.deltaTime, 0.0f, -1f);
}
}
I think you will need another approach, like a simple animation system or state machine. what should happen if a key is released before the cube reaches its snapping destination, or if i switch the key?
Answer by zhijinzhiduzhong · Jun 30, 2021 at 02:50 PM
I have the same needs, and I’m figuring how to use with lerp.
Your answer
Follow this Question
Related Questions
How do i do tactical gridbased movement with movement points? 1 Answer
Drag and Drop Problem 0 Answers
Drag and Drop Problem 1 Answer
Some problem with movement 0 Answers
Dash Movement Platformer 0 Answers