- Home /
How to return to position if key is not pressed?
Hello i want to make a objekt in my game, that changes position if key is pressed and returns to the first position if key is not pressed???
Comment
Best Answer
Answer by clunk47 · Dec 15, 2012 at 09:04 PM
Need to be more specific but I'll give an example in C#. Keep in mind this is a very basic example to get you started.
using UnityEngine;
using System.Collections;
public class EXAMPLE : MonoBehaviour
{
Vector3 oldPosition;
float speed = 5.0f;
void Start()
{
oldPosition = transform.position;
}
void Update()
{
if(Input.GetKey(KeyCode.Space))
transform.Translate(Vector3.forward * speed * Time.deltaTime);
else if(!Input.anyKey)
transform.position = Vector3.MoveTowards(transform.position, oldPosition, speed * Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
How to tell if two blocks are right next to each other?(2D) 1 Answer
2D get touch input 1 Answer
How do I get my door to work? 1 Answer
Get pressed key on smartphone 0 Answers
Returning position of game object in c# 2 Answers