// хочу что бы по нажатию клавиши корабль например резко ускорялся или резко останавливался. Но он это категорически делать не хочет. Вот мой код.
using UnityEngine;
public class PlayerControls : MonoBehaviour {
public float thrustSpeed; // скорость поворота
public float turnSpeed; // скорость движения
public float stop = 1f;
public float hoverPower; // сила зависать
public float hoverHeight; // высота зависания
//private float thrustInput;
//private float turnInput;
private Rigidbody myRigidbody;
void Start()
{
myRigidbody = GetComponent<Rigidbody>();
}
void Update()
{
// ввод с клавиатуры
float thrustInput = Input.GetAxis("Vertical");
float turnInput = Input.GetAxis("Horizontal");
// Поворот корабля
myRigidbody.AddRelativeTorque(0f, turnInput * turnSpeed, 0f);
// Перемещение корабля
myRigidbody.AddRelativeForce(0f, 0f, thrustInput * thrustSpeed);
// Hovering Зависание
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, hoverHeight))
{
float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight;
//Debug.Log(proportionalHeight);
Vector3 appliedHoverForce = Vector3.up * proportionalHeight * hoverPower;
myRigidbody.AddForce(appliedHoverForce, ForceMode.Acceleration);
}
// скрипт огня
if (Input.GetButtonDown("Fire1"))
{
myRigidbody.AddRelativeForce(0f, 0f, thrustInput * stop);
Debug.Log("Fire1 ON " + stop);
}
else if (Input.GetButtonUp("Fire1"))
{
myRigidbody.AddRelativeForce(0f, 0f, thrustInput * thrustSpeed);
Debug.Log("Fire1 OFF" + stop);
}
}
}