I have a few uncertainties here. I basically achieved the basics of moving the object by 1 when one of the directional keys are pressed, and it works fine. However, I also wanted the "Q" (left) and "E" (right) keys to rotate the object 90° to their direction. This is my script, I was unsure how to do the rotation, even after an hour on the Unity Script Reference.
using UnityEngine;
using System.Collections;
public class movement : MonoBehaviour {
public float speed = 2.0f;
private Vector3 endpos;
private bool moving = false;
void Start () {
endpos = transform.position;
}
void Update () {
if (moving && (transform.position == endpos))
moving = false;
if(!moving && Input.GetKey(KeyCode.W)){
moving = true;
endpos = transform.position + Vector3.forward;
}
if(!moving && Input.GetKey(KeyCode.A)){
moving = true;
endpos = transform.position + Vector3.left;
}
if(!moving && Input.GetKey(KeyCode.S)){
moving = true;
endpos = transform.position + Vector3.back;
}
if(!moving && Input.GetKey(KeyCode.D)){
moving = true;
endpos = transform.position + Vector3.right;
}
if(!moving && Input.GetKey(KeyCode.Q)){
moving = true;
endpos = UNSURE WHAT TO PLACE HERE;
}
if(!moving && Input.GetKey(KeyCode.E)){
moving = true;
endpos = UNSURE WHAT TO PLACE HERE;
}
transform.position = Vector3.MoveTowards(transform.position, endpos, Time.deltaTime * speed);
}
}
Thanks in advance! :D
↧