My game is a top down ship game and I really don’t like how movement is turning out. I am using Rigidbody2D.AddRelativeForce , which seems to be my best option for moving based on direction the ship is facing, however I have an annoying consequence of drifting once speed picks up. I want the ship to constantly move forward, which I think is the main problem, because the force never stops being applied. If there is a better option, or a way to tweak AddRelativeForce, I would greatly appreciate some input.
Here is a video of it happening: AddRelativeForce is a diva - Album on Imgur
And here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
Rigidbody2D rb;
[SerializeField]
public float accelerationPower = 1f;
[SerializeField]
public float steeringPower = 1f;
float steeringAmount, direction;
void Start()
{
rb = GetComponent<Rigidbody2D>();
windDirection = compass.GetComponent<Wind>();
}
// Update is called once per frame
void FixedUpdate()
{
steeringAmount = Input.GetAxis("Horizontal");
direction = Mathf.Sign(Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up)));
rb.rotation += steeringAmount * steeringPower * rb.velocity.magnitude * direction;
rb.AddRelativeForce(-Vector2.up * accelerationPower * Time.deltaTime);
rb.AddRelativeForce(-Vector2.right * rb.velocity.magnitude * steeringAmount / 2);
}
}