[UNITY 3D] Moving a ship according to the angle

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.

I finally figured out how to make it work : I used degrees in the Math.Cos and Math.Sin instead of gradients.

        if (Input.GetKey(KeyCode.UpArrow)) {

            x = x + System.Math.Cos(orientation * 6.28 / 360) * 20;
            z = z + System.Math.Sin(orientation * 6.28 / 360) * 20;

            transform.position = new Vector3((float)x, 0, -(float)z);

        }

1- DON’T SLEEP THREAD.
If you want to delay there’s proper co-routines and yield statements for that.

2- You can use the Forward Vector, it’ll give you a unit vector with forward direction instead of doing the math.

Finally, it’s not always a good idea to replace the position, usually interpolating will give smoother movement, also it can cause unexpected behaviors if physics is included.

Hi! Like @Severos_1 said, please don’t sleep threads.

What I assume you are trying to do is slow the input speed of the ship. Instead, change “orientation + 1” out with something like orientation + (speed * Time.deltaTime), this will ensure that its a slower speed, and that it never fluctuates.

You’ll need to define a float speed; somewhere too.

Ok thank to all of you i’ll try you tricks :wink:

Just a question @Severos_1
How do I use the Forward Vector?

So what you want to do is something like:

if (Input.GetKey(KeyCode.UpArrow)) {
  Vector3 move = transform.Forward * speed;
  move.y=0; //to make sure nothing moves on Y, ignore if you're sure forward won't have Y component.
  transform.position += move;
}

Ok thanks