- Home /
Move gameobject to fixed position and back - Using Keypresses
I have made this build where some components of a Booster pump gets shown like in the image below. However now they just gets shown/hidden by pressing the "1" key.
It would be cool if i could make them extend smoothly from the assembled position instead.
Could someone help me modify my script to do that? To extend/move the circled parts smoothly to an fixed position with a keypress and go back when pressing the same key again.
So far I can only make a part keep going in a certain direction, using the speed of my choice, but its a start :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveToPos : MonoBehaviour
{
Vector3 EndPos;
Vector3 StartPos;
public float distPos;
public float MovSpeed;
void Start()
{
StartPos = transform.position;
}
void Update()
{
if (Input.GetKey(KeyCode.UpArrow))
EndPos = transform.position + new Vector3 (0, 0, 1) * MovSpeed * Time.deltaTime;
distPos += 0.01f;
transform.position = Vector3.Lerp(StartPos, EndPos, distPos);
}
}
Answer by Duckocide · Oct 27, 2020 at 02:11 PM
Hi @JesperGreen
There are a few tricks to this, but looking at your code, you perhaps need to consider the "distPos" value and make it Clamp01(...) (Keeps it between 0 and 1) and use the key press to flip the direction. Something like (I've just typed this in here, not tested / syntax checked)...
Extra declarations...
float direction = 1f; // The direction of travel
float maxDistance = 2f; // The maximum move distance
Start()
{
StartPos = transform.position;
EndPos = StartPos + Vector3.forward * maxDistance;
}
Update()
{
if (Input.GetKey(KeyCode.UpArrow))
direction = 1f;
else
direction = -1f;
distPos = Mathf.Clamp01(distPos + (time.deltaTime * moveSpeed * direction));
transform.position = Vector3.Lerp(StartPos, EndPos, distPos);
}
Hope this helps :)
Hi @Duckocide Thank you for your help :)
The script below works, with your help, but i have 2 issues with it.
The object starts with moving without keypress.
I would like the object to move to position on keypress and then back with keypress on same key again.
I think I might need some true/false statements? But i am not sure on how.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Clp$$anonymous$$ove : $$anonymous$$onoBehaviour
{
Vector3 EndPos;
Vector3 StartPos;
public float distPos;
public float $$anonymous$$ovSpeed;
public float direction = 1f; // The direction of travel
public float maxDistance = 2f; // The maximum move distance
void Start()
{
StartPos = transform.position;
EndPos = StartPos + Vector3.forward * maxDistance;
}
void Update()
{
if (Input.GetKey(KeyCode.UpArrow))
direction = 1f;
if (Input.GetKey(KeyCode.DownArrow))
direction = -1f;
distPos = $$anonymous$$athf.Clamp01(distPos + (Time.deltaTime * $$anonymous$$ovSpeed * direction));
transform.position = Vector3.Lerp(StartPos, EndPos, distPos);
}
}
Hi @JesperGreen
OK, so you can change the key press logic, add something to check when distPos is at its limits and change direction float to 0f when it is.
Something like...
public direction = 0f;
void Update()
{
if (Input.GetKey(KeyCode.UpArrow) && distPos = 1f)
direction = -1f;
if (Input.GetKey(KeyCode.UpArrow) && distPos = 0f)
direction = 1f;
distPos = $$anonymous$$athf.Clamp01(distPos + (Time.deltaTime * $$anonymous$$ovSpeed * direction));
If (distPos == 0f || distPos = 1f) direction = 0f;
transform.position = Vector3.Lerp(StartPos, EndPos, distPos);
}
Now, as ever there are a few ways to skin a cat on this (not least of which is using Unity's animation controls). However, I hope this helps illustrate what you can do with the math. It's a bit CPU wasteful doing the distPos calculation all the time (when direction is 0). You could also do a check to see if direction is anything other than 0 (another if statement).
The script fails at:
if (Input.GetKey(KeyCode.UpArrow) && distPos = 1f)
direction = -1f;
"Operator && cannot be applied to operands of type bool and float"
Your answer
Follow this Question
Related Questions
Moving child and parent 1 Answer
dynamic rotation distance 1 Answer
I need help about raycast and edges 2 Answers
Shader problem: length() en distance() 1 Answer
Simple way to get all objects (rigidbodys) around x distance from the player 1 Answer