I am trying to learn how to move objects in Unity without using the built in features like AddForce etc. Some tutorials on Unity website (and other places) is where I have got most of my 'knowledge' so far from.
This script for moving players (in topdown Space SHMUP) has been working fine for me and includes acceleration and artificial drag (basically 'smooth' movement, or like being on ice).
I'm sure this code is overly long, bloated, inefficient and in most cases downright wrong, but it is written for me to understand it, rather than simply using Unity Prefabs and AssetPackages i download from Store.
QUESTION: Do I need the Time.fixedDeltaTime mutlipiers when calculating my 'delta_v' values here. And also what about on the drag part at the bottom.
As usual all help and comments are greatly appreciated. Thanks!
public class Player_Movement_SpaceShooter_TopDown : MonoBehaviour
{
public Vector2 maxVelocity;
public float drag;
public float moveForce;
private Rect moveBounds;
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
moveBounds = Game_Manager.instance.ScreenBounds;
}
private void FixedUpdate()
{
float input_h = Input.GetAxis("Horizontal");
float input_v = Input.GetAxis("Vertical");
Vector2 delta_v = Vector2.zero;
delta_v.x += input_h * moveForce * Time.fixedDeltaTime;
delta_v.x = Mathf.Clamp(delta_v.x, -maxVelocity.x, maxVelocity.x);
delta_v.y += input_v * moveForce * Time.fixedDeltaTime;
delta_v.y = Mathf.Clamp(delta_v.y, -maxVelocity.y, maxVelocity.y);
Vector2 pos = rb.position;
Vector2 vel = rb.velocity;
if (pos.x < moveBounds.xMin)
{
pos.x = moveBounds.xMin;
vel.x = 0f;
}
if (pos.x > moveBounds.xMax)
{
pos.x = moveBounds.xMax;
vel.x = 0f;
}
if (pos.y < moveBounds.yMin)
{
pos.y = moveBounds.yMin;
vel.y = 0f;
}
if (pos.y > moveBounds.yMax)
{
pos.y = moveBounds.yMax;
vel.y = 0f;
}
rb.position = pos;
rb.velocity = vel + delta_v;
rb.velocity = rb.velocity * (1 - Time.fixedDeltaTime * drag);
}
}
It appears to me that if FixedUpdate was somehow slowed down, the velocity would get changed slower, the same with the enemy movement but I don't know how that would effect the gameplay and if/how the slowing-down would occur