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! ![]()
It returns the error: Assets/movement.cs(40,44): error CS1061: Type
– DubstepDragonUnityEngine.Transform' does not contain a definition forrotate' and no extension methodrotate' of typeUnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?) Also, no matter how much I increase the speed, the movement speed does not increase...Also, how do I get it to use local transform instead of global? So as to move forward even though the object was rotated.
– DubstepDragontechnically its transform.Rotate I just assumed you'd get the autocomplete the local is transform.forward; global is vector3.forward
– sparkzbarcaThanks very much! Oh and my bad, sorry :P
– DubstepDragon