- Home /
This question was
closed Sep 22, 2013 at 06:54 AM by
Fattie for the following reason:
Duplicate Question
Question by
udmsoft · Sep 22, 2013 at 02:41 AM ·
positionmovemove object
Will this code move an object
using UnityEngine;
using System.Collections;
public class move : MonoBehaviour {
// Use this for initialization
void Start () { }
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.UpArrow)) {
transform.position.Set(transform.position.x + 5,transform.position.y,transform.position.z);
}
if (Input.GetKey (KeyCode.DownArrow)) {
transform.position.Set(transform.position.x - 5,transform.position.y,transform.position.z);
}
if (Input.GetKey(KeyCode.LeftArrow)){
transform.position.Set(transform.position.x,transform.position.y,transform.position.z + 5);
}
if (Input.GetKey(KeyCode.LeftArrow)){
transform.position.Set(transform.position.x,transform.position.y,transform.position.z - 5);
}
}
}
Please figure out how to do it
Comment
Best Answer
Answer by VioKyma · Sep 22, 2013 at 04:34 AM
Set() will not work because transform.position issues a copy of itself.
To do this, you will need to create a new Vector3 and then assign it to transform.position.
eg
if (Input.GetKey (KeyCode.UpArrow)) {
Vector3 move = new Vector3(transform.position.x + 5,transform.position.y,transform.position.z);
transform.position = move;
}
To get smoother, more consistent movement, you might want to also use Time.deltaTime to make your movement relative to time passed between each update.
Answer by DaveA · Sep 22, 2013 at 02:45 AM
In answer to your question, yes. In answer to your request, what?