Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Unity, my FPS character accelerates when going diagonally

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

public class PlayerMovment : MonoBehaviour
{
    private CharacterController Controller;


    public float speed = 12f;
    public float gravity = -9.81f * 2;
    public float JumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;

    bool isGrounded;
    bool isMoving;

    private Vector3 lastPosition = new Vector3(0f,0f,0f);

    void Start()
    {
        Controller = GetComponent<CharacterController>();
    }


    void Update()
    {

        //Karakter yere deÄźiyormu.
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        Debug.Log("Birim karedeki hız: " + Controller.velocity.magnitude);
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }


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


        Vector3 move = transform.right * x + transform.forward * z;

        Controller.Move(move*speed*Time.deltaTime);

        if(Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y += Mathf.Sqrt(JumpHeight * -2f * gravity);
        }
        
        velocity.y += gravity*Time.deltaTime;


        Controller.Move(velocity*Time.deltaTime);

        if(lastPosition != gameObject.transform.position && isGrounded == true)
        {
            isMoving = true;
        }
        else
        {
            isMoving = false;
        }

    }
}

I am trying to make an FPS game, the code above is the character’s movement codes, but the character speeds up when going diagonally. The reason for this problem is that the speed of the character increases when trying to go in two directions while going diagonally or something else? Also, how do I measure the speed of the character, the speed it travels per unit frame.GAME ENGINE UNITY

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This isn’t a perfect solution, but it’s pretty good.

Vector2 inVector = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

if(inVector.magnitude > 1) { inVector.Normalize(); }

Vector3 move = transform.right * inVector.x + transform.forward * inVector.y;

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading