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;
}
}
}
Estou tentando fazer um jogo FPS, o código acima são os códigos de movimento do personagem, mas o personagem acelera ao andar na diagonal. A razão para este problema é que a velocidade do personagem aumenta ao tentar ir em duas direções enquanto vai na diagonal ou algo mais? Além disso, como posso medir a velocidade do personagem, a velocidade que ele viaja por unidade de quadro.UNIDADE DO MOTOR DE JOGO