- Home /
Translation.forward in Conjunction with Boats
Hello,
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);
}
} `
Answer by Kishotta · May 19, 2017 at 12:46 AM
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.
Try:
transform.Translate (speed * transform.forward * Time.deltaTime, Space.World);
to get transform.forward
in world space coordinates.
Answer by Marshal_Alessi · May 19, 2017 at 10:59 AM
I ended up using
transform.Translate(Vector3.forward Time.deltaTime speed);
Which worked, though the other answer works as well.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Condition on Character Movement 1 Answer
Movement Problems PS4 Controller 0 Answers
How to call two animation Controller? 0 Answers