#Slippery top down movement

1 messages · Page 1 of 1 (latest)

prime knot
#

Hey I am currently making my first game, it's going to be a simple top down shooter where you're a spaceship. I followed Brackeys tutorial on movement but since it is made for people not ships it's not the same. I want my movement to be slippery and have acceleration like a real vehicle, how would I do this?

This is the code from brackeys video ```using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
// Variables

public float moveSpeed = 5f;

public Rigidbody2D rb;
public Camera cam;

Vector2 movement;
Vector2 mousePos;

void Update()
{
    movement.x = Input.GetAxisRaw("Horizontal");
    movement.y = Input.GetAxisRaw("Vertical");

    mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}

void FixedUpdate() 
{
    rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    
    Vector2 lookDir = mousePos - rb.position;
    float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
    rb.rotation = angle;
}

}```

stiff forge
#
  1. For acceleration you would have to mutliply your the acceleration to your moveSpeed
    rb.MovePosition(rb.position + movement * moveSpeed * acceleration * Time.fixedDeltaTime);
#
  1. For slippery feel, instead of using MovePosition consider using rb.AddForce(Force to be applied)
#

Ping me if you have questions

prime knot
#

Thank you so much!

prime knot
grave pulsar
#

AddForce literally pushes an object's Rigidbody. If nothing works to keep 7t balanced, it will fall over. To keep it upright, you should constrain the Rb's rotation on both horizontal axes. And if you don't turn your ship to the sides (like in upward-scrolling shooter), constraining the Y-rotation is also a good idea.

#

Also, if you use AddForce in default mode, it will apply the force as long as you keep the input (for example keep the button pressed), so it'll accelerate (mitigated only by drag).
If you turn off the input (release the button), it'll stop applying force - moving at the last elapsed speed (if no drag) or gradually slowing down (with drag). Unless it hits another object, in which case you can work with Colliders and Physics Materials to account for any extra effects (such as ramming an enemy or crashing into an obstacle)