Кораблик немного непослушный, кого это не устраивает может дописать под себя, скрипт не очень сложный.
Не забудьте добавить в Input axis-ы управления "Pitch", "Roll", "Yaw" u "Up". "Pitch" это Y-axis (mouse movement), "Yaw" это X-axis (mouse movement), "Roll" u "Up" x-axis (key or mouse button - сюда назначаем кнопки).
Затем кидаем скрипт на камеру (или куда хотите) и назначаем объекту ригидбоди и коллайдер. Всё.
Не забудьте запереть вращение (freez rotation) на ригидбоди, иначе кораблик будет жутко мотать туда сюда при столкновении с любой стенкой.
СurrentSpeed это наша текущая скорость, на её основе можно сделать повреждения, если надо.
HTML код для вашего блога :
Ах да, ещё:
space вверх
cntrl вниз
Синтаксис:
Используется csharp
using UnityEngine;
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public Transform moving;
public float maxspeed = 10.0f;
public float force = 15f;
public float currentSpeed; //наша текущая скорость, на её основе можно сделать повреждения
// Вращение
public float smoothRotation = 5f;
public bool lockedConst; // If this is checked, it locks pitch roll and yaw const to the var rotationConst.
public int rotationConst = 10;
public float pitchConst = 1000f;
public float rollConst = 100f;
public float yawConst = 1000f;
public float pitchDelta = 12f;
public float rollDelta = 12f;
public float yawDelta = 12f;
private float trueSmooth;
private float truePitch;
private float trueRoll;
private float trueYaw;
private float bankingAngle;
private float mouseX;
private float mouseY;
public bool water = false;
void Start(){
// GetComponent<CharacterController>().enabled = false;
if (lockedConst == true)
{
pitchConst = rotationConst;
rollConst = rotationConst;
yawConst = rotationConst;
Screen.showCursor = false;
}
}
public void Awake(){
}
public void Update () {
// * * This section of code handles the plane's rotation.
var pitch = -Input.GetAxis ("Pitch") * pitchConst;
var roll = Input.GetAxis ("Roll") * rollConst;
var yaw = -Input.GetAxis ("Yaw") * yawConst;
pitch *= pitchDelta * Time.deltaTime;
roll *= -rollDelta * Time.deltaTime;
yaw *= yawDelta * Time.deltaTime;
// Smothing Rotations...
trueSmooth = Mathf.Lerp (trueSmooth, smoothRotation, 5* Time.deltaTime);
truePitch = Mathf.Lerp (truePitch, pitch, trueSmooth * Time.deltaTime);
trueRoll = Mathf.Lerp (trueRoll, roll, trueSmooth * Time.deltaTime);
trueYaw = Mathf.Lerp (trueYaw, yaw, trueSmooth * Time.deltaTime);
if(water){
Vector3 temp = moving.transform.position;
temp.y = moving.transform.position.y + 0.001f;
moving.transform.position = temp;
}
}
public float horizontal
{
get
{
return Input.GetAxis("Horizontal") * force;
}
}
public float vertical
{
get
{
return Input.GetAxis("Vertical") * force;
}
}
public float Up
{
get
{
return Input.GetAxis("Up") * force;
}
}
// Now we apply what we calculated...
void FixedUpdate () {
currentSpeed = rigidbody.velocity.magnitude;
rigidbody.AddForce (transform.rotation * Vector3.forward * vertical);
rigidbody.AddForce (transform.rotation * Vector3.right * horizontal);
rigidbody.AddForce (transform.rotation * Vector3.up * Up);
transform.Rotate (truePitch,-trueYaw,trueRoll);
if(currentSpeed > maxspeed)
rigidbody.velocity = rigidbody.velocity.normalized * maxspeed;
}
}
}
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public Transform moving;
public float maxspeed = 10.0f;
public float force = 15f;
public float currentSpeed; //наша текущая скорость, на её основе можно сделать повреждения
// Вращение
public float smoothRotation = 5f;
public bool lockedConst; // If this is checked, it locks pitch roll and yaw const to the var rotationConst.
public int rotationConst = 10;
public float pitchConst = 1000f;
public float rollConst = 100f;
public float yawConst = 1000f;
public float pitchDelta = 12f;
public float rollDelta = 12f;
public float yawDelta = 12f;
private float trueSmooth;
private float truePitch;
private float trueRoll;
private float trueYaw;
private float bankingAngle;
private float mouseX;
private float mouseY;
public bool water = false;
void Start(){
// GetComponent<CharacterController>().enabled = false;
if (lockedConst == true)
{
pitchConst = rotationConst;
rollConst = rotationConst;
yawConst = rotationConst;
Screen.showCursor = false;
}
}
public void Awake(){
}
public void Update () {
// * * This section of code handles the plane's rotation.
var pitch = -Input.GetAxis ("Pitch") * pitchConst;
var roll = Input.GetAxis ("Roll") * rollConst;
var yaw = -Input.GetAxis ("Yaw") * yawConst;
pitch *= pitchDelta * Time.deltaTime;
roll *= -rollDelta * Time.deltaTime;
yaw *= yawDelta * Time.deltaTime;
// Smothing Rotations...
trueSmooth = Mathf.Lerp (trueSmooth, smoothRotation, 5* Time.deltaTime);
truePitch = Mathf.Lerp (truePitch, pitch, trueSmooth * Time.deltaTime);
trueRoll = Mathf.Lerp (trueRoll, roll, trueSmooth * Time.deltaTime);
trueYaw = Mathf.Lerp (trueYaw, yaw, trueSmooth * Time.deltaTime);
if(water){
Vector3 temp = moving.transform.position;
temp.y = moving.transform.position.y + 0.001f;
moving.transform.position = temp;
}
}
public float horizontal
{
get
{
return Input.GetAxis("Horizontal") * force;
}
}
public float vertical
{
get
{
return Input.GetAxis("Vertical") * force;
}
}
public float Up
{
get
{
return Input.GetAxis("Up") * force;
}
}
// Now we apply what we calculated...
void FixedUpdate () {
currentSpeed = rigidbody.velocity.magnitude;
rigidbody.AddForce (transform.rotation * Vector3.forward * vertical);
rigidbody.AddForce (transform.rotation * Vector3.right * horizontal);
rigidbody.AddForce (transform.rotation * Vector3.up * Up);
transform.Rotate (truePitch,-trueYaw,trueRoll);
if(currentSpeed > maxspeed)
rigidbody.velocity = rigidbody.velocity.normalized * maxspeed;
}
}
}
P.S. Пока что кораблик может разгоняться до бесконечности, ещё не придумал как это исправить. >>> !Разобрался! Обновил скрипт