Translation.forward in Conjunction with Boats


I’m making a boat game which(obviously) allows the player to drive a boat. But I’m having a tad bit of an issue with the player moving forward. I used transform.forward to allow them to head in the direction they are facing, but it doesn’t quite work as expected. I drew a small picture to demonstrate, but basically the boat is heading in the RED direction instead of staying aligned heading the BLUE direction.

Here’s my code:`using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class boatControllerv2 : MonoBehaviour {

float speed = 0f;
float maxSpeed = 10f;
float accel = .2f;
bool _isMoving = false;

// Use this for initialization
void Start () {

// Update is called once per frame
void Update ()
    Debug.DrawRay(transform.position, transform.forward);
    _isMoving = false;
    if (Input.GetKey(KeyCode.W))
        _isMoving = true;
        speed += accel;
    if (speed > maxSpeed)
        speed = maxSpeed;
    if(!_isMoving && speed > 0)
        speed -= 5 * Time.deltaTime;
    if(_isMoving && Input.GetKey(KeyCode.Q))
        transform.Rotate(-transform.up * 20 * Time.deltaTime);
    transform.Translate(speed * transform.forward * Time.deltaTime);


transform.Translate (v) moves an object with v in world space. So since transform.forward is <0, 0, 1> in local space it will move in the world’s positive z direction.


transform.Translate (speed * transform.forward * Time.deltaTime, Space.World);

to get transform.forward in world space coordinates.

I ended up using

transform.Translate(Vector3.forward * Time.deltaTime * speed);

Which worked, though the other answer works as well.