Uneven values

In my game, I have a jet that rotates around the player (following the mouse) and propels the player in the opposite direction of where it’s facing when you click and hold. The problem is, the jet propels the player with about 10 times as much force in the y axis. What could be causing this?

This is the snippet of code I’m using to add the force:

        if (Input.GetKey(KeyCode.Mouse0))
        {
            rigid.AddForce((engine.transform.right.normalized * -force) * Time.deltaTime);
        }

rigid is the player Rigidbody, engine is the GameObject for the jet, and force is a public float (This is in 2D).

Still haven’t solved this…

Sounds like you’re using physics stuff from within Update. Anything that impacts physics should be in FixedUpdate.

I believe that AddForce also shouldn’t use Time.deltaTime.

Nope, that didn’t work.

Here’s a gif of what’s happening, if that helps:

just fyi “engine.transform.right” is already normalized.

Nothing looks wrong with your sample, but it’s hard to diagnose without more context. There must be something else affecting the force vector. Are you certain your engine’s transform rotation is being set correctly, and not somehow pointing partially down the Z axis or something?

Try to debug draw the force vectors and velocity to see the direction and magnitude of the force being added and the resulting velocity, the issue may reveal itself.

Looks like friction to me. Try using a physics material without any friction and see what happens.

This is a gif of a debug.draw of the velocity:

I’ve found through debugging that it seems to not count the X movement by the jet as velocity. It counts walking and stuff like normal but it just comes up as zero on the X axis if I use the jet, even though it’s clearly moving.

Are you explicitly setting velocity anywhere? If you set velocity, it negates all current velocity applied using AddForce.

Hmm, that’s probably from my movement script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Movement : MonoBehaviour
{
    public float MoveSpeedMax;
    public float JumpSpeed;
    public float fallFast;
    public float lowJump;
    public float slowDown;
    public float Rotate;
    public int Jumps;
    public int maxJumps;
    public bool Grounded;
    public Camera cameraM;
    public GameObject Weapon1;
    public Vector3 MousePos;
    public Vector3 pointPos;

    Vector2 PlayerVelocity;
    private Rigidbody2D RigidbodyComponent;

    private void Awake()
    {
        RigidbodyComponent = GetComponent<Rigidbody2D>();
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        Jumps = 0;
    }

    // Use this for initialization
    void Start()
    {
        PlayerVelocity = Vector3.zero;
    }

    // Update is called once per frame
    void Update()
    {
        MousePos = Input.mousePosition;
        MousePos.z = -10;
        pointPos = cameraM.WorldToScreenPoint(Weapon1.transform.position);
        MousePos.x = MousePos.x - pointPos.x;
        MousePos.y = MousePos.y - pointPos.y;
        Rotate = Mathf.Atan2(MousePos.y, MousePos.x) * Mathf.Rad2Deg;
        Weapon1.transform.rotation = Quaternion.Euler(new Vector3(0, 0, Rotate));

        PlayerVelocity.x = Input.GetAxis("Horizontal") * MoveSpeedMax;

        if (RigidbodyComponent.velocity.y < 0)
        {
            RigidbodyComponent.velocity += Vector2.up * Physics2D.gravity.y * (fallFast - 1) * Time.deltaTime;
        }
        else if (RigidbodyComponent.velocity.y > 0 && (!Input.GetKey(KeyCode.Space) && !Input.GetKey(KeyCode.W)))
        {
            RigidbodyComponent.velocity += Vector2.up * Physics2D.gravity.y * (lowJump - 1) * Time.deltaTime;
        }

        if (Jumps < maxJumps && (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.W)))
        {
            PlayerVelocity.y = JumpSpeed;
            Jumps++;
        }
        else
        {
            PlayerVelocity.y = RigidbodyComponent.velocity.y;
        }
        RigidbodyComponent.velocity = PlayerVelocity;
    }
}

How should I fix this?

Instead of using velocity +=, try using AddForce instead.

You won’t be able to keep and alter the velocity directly. All you can do is AddForce and then the physics simulation will determine the velocity for you.

It’s the “RigidbodyComponent.velocity = PlayerVelocity;” line at the end that’s causing this, but if I just get rid of it then I can’t walk around, and if I change it to AddForce then the walking is extremely slidy. Are there any alternatives to AddForce that doesn’t have weird acceleration and doesn’t directly modify the velocity?

Try to give your player’s rigidbody or collider a high friction physics material. That may serve to stop quickly on surfaces without changing other movement behavior.