Черный экран Unity3D
Пытался реализовать свой ФПС контроллер при помощи rb.velocity, но у меня появляется черный экран. Ошибка появилась на том моменте, когда я попытался сделать так, что бы персонаж двигался в направление взгляда. Вот код
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 10f;
public float jumpForce = 5f;
public float gravity = -9.81f;
public Transform cameraTransform;
public float lookSpeed = 2f;
private Rigidbody rb;
private Vector3 moveDirection = Vector3.zero;
private float rotationX = 0f;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.FreezeRotation;
}
void FixedUpdate()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(x, 0, z);
if (IsGrounded())
{
if (Input.GetButtonDown("Jump"))
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
else
{
direction.y = rb.velocity.y;
}
rb.velocity = transform.TransformDirection(direction) * moveSpeed;
}
bool IsGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, 0.1f);
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * lookSpeed;
rotationX += Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -90f, 90f);
transform.rotation = Quaternion.Euler(0, mouseX, 0);
cameraTransform.localRotation = Quaternion.Euler(-rotationX, 0, 0);
}
}
Источник: Stack Overflow на русском