Как добавить "прыжок" в С# скрипт на Unity 3D с помощью Character Controller?
Сделал скрипт передвижения персонажа в Unity с плавным поворотом в 8 направления, но не знаю как добавить прыжок в скрипт и ничего не сломать.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public CharacterController controller;
public float speed;
float turnSmoothVelocity;
public float turnSmoothTime;
void Update() {
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f) {
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
controller.Move(direction * speed * Time.deltaTime);
}
}
}
Ответы (2 шт):
Автор решения: Andrew
→ Ссылка
Судя по документации самого юнити https://docs.unity3d.com/ScriptReference/CharacterController.Move.html
(кстате там много всего интересного есть, советую к прочтению)
void Update()
{
if (characterController.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
}
}
но лучше читать саму документацию, там ответ шире.
Автор решения: Иван
→ Ссылка
if (Input.GetButtonDown("Jump"))
{
if (_charController.isGrounded)
{
movement.y = jumpHeight;
}
}