Контроллер проигрывателя UnityC#

Место общения программистов C#
Гость
Контроллер проигрывателя Unity

Сообщение Гость »


I'm having a problem with applying velocity to my character controller and using Vector3.normalize to make diagonal movement the same speed as forward movement. When I use Vector3.normilize it normalizes x, y, and z. I need it to only normalize x, and z so that I can increase y more than 1 or -1, making so that I can make gravity pull you down faster the longer you are falling, eventually reaching terminal velocity.

Код: Выделить всё

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

public class PlayerMovementScript : MonoBehaviour
{
public Vector3 moveDirection = new Vector3();

public float moveSpeed;
public float velocity;
public float terminalVelocity;
CharacterController characterController;

// Start is called before the first frame update
void Start()
{
if (characterController == null)
{
characterController = GetComponent();
}
}

// Update is called once per frame
void Update()
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), moveDirection.y, Input.GetAxis("Vertical"));
// moveDirection = moveDirection.normalized;
if (!characterController.isGrounded)
{
if (moveDirection.y > -terminalVelocity)
{
moveDirection.y -= Time.deltaTime * velocity;
}
} else
{
moveDirection.y = -2;
}
characterController.Move(moveDirection * Time.deltaTime * moveSpeed);
}
}
When I run this code the gravity works, but the diagonal movement doesn't. When I run the code with the commented out moveDirection.normalize diagonal movement works, but the gravity doesn't. How do I fix this?


Источник: https://stackoverflow.com/questions/781 ... controller

Вернуться в «C#»