E
E
edisonvanek2022-03-07 18:37:43
C++ / C#
edisonvanek, 2022-03-07 18:37:43

unity. C#. The character moves globally, but it should be local. How to do it?

I'm making a game in first person, but when the character is rotated, `Player.transform.Translate` moves it globally. Tried through rigidbody, but did not help. How to fix it?

using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [Header("Player")]
    public GameObject Player;

    [Header("Move")]
    public bool Move;
    public float MoveSpeed;

    private Vector3 MoveDir;

    private float PlayerSpeed;

    [Header("Run")]
    public bool Run;
    public float RunSpeed;


    [Header("Jump")]
    public bool Jump = true;
    public float JumpForce = 4f;
    [HideInInspector] public bool IsGround;

    void Awake() {
        
        if (!Move) {
            MoveSpeed = 0;
        }
        
        if (!Run) {
            RunSpeed = MoveSpeed;
        }
    }

    private void FixedUpdate() {

        float Horizontal = Input.GetAxis("Horizontal");
        float Vertical = Input.GetAxis("Vertical");

        if (Input.GetAxis("Fire3") == 0) {
            PlayerSpeed = MoveSpeed;
        }

        if (Input.GetAxis("Fire3") > 0) {
            PlayerSpeed = RunSpeed;
        }
        MoveDir = new Vector3(Horizontal * PlayerSpeed, 0f, Vertical * PlayerSpeed);
        Player.transform.Translate(MoveDir);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Farawa, 2022-03-07
@edisonvanek

transform.forward and transform.right

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question