Hi everyone,
I started coding with Unity 5 and the C# language a few days ago and I’m searching for a way to move a ship towards the direction he’s facing.
I’ve done this, but it’s doing weird things (not the expected behavior actually) :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCube : MonoBehaviour {
public float orientation = 0;
public float speed = 1.5f;
public Vector3 newPosition = Vector3.zero;
public double x = 0;
public double z = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CheckKeys();
System.Threading.Thread.Sleep(16);
}
void CheckKeys() {
if (Input.GetKey(KeyCode.RightArrow)) {
orientation = orientation + 1;
if (orientation == 360) {
orientation = 0;
}
transform.rotation = Quaternion.AngleAxis(orientation, Vector3.up);
}
if (Input.GetKey(KeyCode.UpArrow)) {
z = z + System.Math.Cos(orientation) * 20;
x = x + System.Math.Sin(orientation) * 20;
transform.position = new Vector3((float)x, 0, (float)z);
}
if (Input.GetKey(KeyCode.LeftArrow)) {
orientation = orientation - 1;
if (orientation == -360) {
orientation = 0;
}
transform.rotation = Quaternion.AngleAxis(orientation, Vector3.up);
}
}
}
Thanks for your help.