AddForce moves character in the wrong direction.

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

public class PlayerMovementController : MonoBehaviour
{
    [SerializeField] private float speed;
    [SerializeField] private float normalspeed;
    [SerializeField] private float jumpForce;
    [SerializeField] private float nextTimeToStrafe = 0;
    [SerializeField] private float strafeRate = 2f;
    [SerializeField] public bool onGround = true;
    [SerializeField] private const int MAX_Jump = 2;
    [SerializeField] private float fuel = 100f;
    [SerializeField] private float fuelcost = 20;
    [SerializeField] private float fuelrechargeamount = 5f;
    [SerializeField] private float rechargeTimer = 2f;
    [SerializeField] private float rechargeTime = 2f;
    [SerializeField] private bool walljump = false;
    [SerializeField] private float thrust = 2f;
    [SerializeField] private float actionsdelay;
    [SerializeField] private float actiondelayTimer;
    [SerializeField] private float dash = 5;
    [SerializeField] public bool iscrouching;
    [SerializeField] public float characterheight = 2f;
    [SerializeField] Animator animator;
    [SerializeField] public float crouchheight = 1.4f;

    public float hAxis;
    public float vAxis;
    public bool ismoving;

    public bool ismage;

    public float hvalue;
    public float currenthealth;

    [Header("Fall Damage")]
    public float startYPos;
    public float endYPos;
    public float fallthreshold = 5;
    public float falldamage;

    public Rigidbody rb;
    private CapsuleCollider cs;

    [Header("OtherScripts")]
    public PlayerVitals vs;

    private void Start()
    {
        speed = 3f;
        rb = GetComponent<Rigidbody>();
        cs = GetComponent<CapsuleCollider>();
        iscrouching = false;
        animator = gameObject.GetComponent<Animator>();
    }

    private void Update()
    {
        Jump();
        Dash();
        Crouch();
        Sprint();
        Fuel();
        Thrust();
    }

    private void Sprint()
    {
        if(Input.GetKey(KeyCode.LeftShift))
        {
            speed = 6;
            animator.SetFloat("speed", speed);
        }
        else
        {
            speed = 3;
            animator.SetFloat("speed", speed);
        }
    }

    public void Falldamage()
    {
        if(!onGround)
        {
            falldamage = startYPos - endYPos;

            if (transform.position.y < startYPos)
            {
                endYPos = transform.position.y;

                if(endYPos < startYPos)
                {

                    if(startYPos - endYPos > fallthreshold)
                    {
                        vs.maxHealth -= falldamage;
                    }

                }
            }          
        }

        if(onGround)
        {
            startYPos = transform.position.y;
        }
    }

    private void Crouch()
    {
        if (Input.GetKey("c"))
        {
            cs.height = .9f;
            iscrouching = true;
        }
        else
        {
           
        }
    }

    private void Delay()
    {
        if(actiondelayTimer > 0)
        {
            actiondelayTimer -= Time.deltaTime;
        }
        if(actiondelayTimer <= 0)
        {
            actiondelayTimer = actionsdelay;
        }
    }

    private void Dash()
    {
        if ((Input.GetKeyDown(KeyCode.LeftShift)) && iscrouching == true)
        {
            rb.AddForce(0, 0, dash, ForceMode.Impulse);
        }
        if (Input.GetKeyDown("x"))
        {
            cs.height =  crouchheight;
            rb.AddForce(0, 0, -dash, ForceMode.Impulse);
        }
        else
        {
            cs.height = characterheight;
        }
    }

    private void Jump()
    {
        if (Input.GetKeyDown(KeyCode.Space) && onGround)
        {
            animator.SetTrigger("jump");
            rb.AddForce(0, jumpForce, 0, ForceMode.Impulse);
            onGround = false;          
        }
        else
        {

        }
    }

    void OnCollisionEnter(Collision Collision)
    {
        if(Collision.gameObject.tag == "Ground")
        {
            onGround = true;
        }
    }

    private void FixedUpdate()
    {
        Move();
    }

    private void Move()
    {
        hAxis = Input.GetAxisRaw("Horizontal");
        vAxis = Input.GetAxisRaw("Vertical");

        animator.SetFloat("hAxis", hAxis);
        animator.SetFloat("vAxis", vAxis);

        Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.deltaTime;

        Vector3 newPosition = rb.position + rb.transform.TransformDirection(movement);
        Quaternion rotation = Quaternion.Euler(movement);
       
        rb.MovePosition(newPosition);
        rb.rotation = Quaternion.identity;
    }

    private void Fuel()
    {
        if (rechargeTimer > 0)
        {
            rechargeTimer -= Time.deltaTime;
        }

       if (rechargeTimer <= 0)
        {
            fuel += fuelrechargeamount;
            rechargeTimer = rechargeTime;
        }
    }

    private void Thrust()
    {
        if (Input.GetKeyDown("j") && fuel >= 20)
        {
            rb.AddForce(0, thrust, 0, ForceMode.Impulse);
            fuel -= fuelcost;
        }
        if (Input.GetKeyDown("q") && fuel >= 20)
        {
            animator.SetTrigger("roll left");
            rb.AddRelativeForce(-Vector3.right * thrust);
            fuel -= fuelcost;
        }
        if (Input.GetKeyDown("e") && fuel >= 20)
        {
            animator.SetTrigger("roll right");
            rb.AddRelativeForce(Vector3.right * thrust);
            fuel -= fuelcost;
        }
     
    }
}

My thrust function moves my character perfectly on play but after moving to different angles. It screws everything up. Also my jump works perfectly but thrust once again takes like 2000+ to move my character decently. It is just gravity or a settings issue on my end.

No one is going to be able to help if the most detailed explanation of the problem you can provide is “It screws everything up.”

1 Like

Whats going wrong? The force applied?
The animation?
If its the animation check your trigger path, it might not return to the Idle state in some circumstances.

Edit: just re-saw the title, lol

What is the actual behaviour your seeing?

1 Like

On play it works perfectly but once I rotate around the world the direction inverse for some reason. Like right becomes left and left becomes right. I also go forward or backwards, I think it’s dependent on world space though. Transform.position is suppose to take the transform axis and not the world axis. So I’m not sure why it’s fucked up. I’ve tried transform.position and Relative force but neither fixed anything. Not really sure what’s going wrong.

I just want q and e to make me go left or right at all times not forward, left, right, or backwards randomly. I also have to set thrust to like 10k to move my character a reasonable distance not sure why.

Transform.position is the world space position. It does not take any rotation of the object into account.

Why… why is this a thing in your “Move” code?

        Quaternion rotation = Quaternion.Euler(movement);

you’re not using it and just setting the rotation to zero (Quaternion.Identity) every frame.

the trust function seems to be in order, the first AddForce will always push in global Y(up), other two that use the relative variation should work as well, what looks off to me is the Dash function, you always dash in global Z, no matter the player orientation, use AddRelativeForce to apply it in the player’s Z (or use the player transform.forward)

Also, I’m not sure how this works so I may be wrong, but I think the rigidbody function MovePosition messes around with the velocity and maybe giving you weird results by overriding the force you’re adding.