Question by
szabogergely · May 03, 2018 at 02:31 PM ·
physicscontrol
BEGINNER: Plane stabilaze issue.
Hello! I want to make a game with a rocket/plane flying around controlled by the user. I want to slowly stabilaze the plane when it turns. Heres my script, and thanks fo helping:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playerMovement : MonoBehaviour {
public Rigidbody player;
public Transform starterPos;
public Vector3 movementSpeed;
public float smoothAmountRotation;
public float turnSpeeed;
public float smoothSpeed;
void FixedUpdate ()
{
PlayerControl();
//Gravity - flight physics
player.AddRelativeForce(-10f*Time.deltaTime, 0, 0);
player.transform.Rotate(0, 0, 3 * Time.deltaTime);
Stabilaze();
}
public void PlayerControl()
{
if (Input.GetKey(KeyCode.W))
{
player.AddRelativeForce(movementSpeed * Time.deltaTime, ForceMode.VelocityChange);
}
if (Input.GetKey(KeyCode.A))
{
//rotate around +X axis
player.transform.Rotate(0, turnSpeeed * Time.deltaTime, 0, Space.Self);
}
if (Input.GetKey(KeyCode.D))
{
//rotate around -X axis
player.transform.Rotate(0, -turnSpeeed * Time.deltaTime, 0, Space.Self);
}
if (Input.GetKey(KeyCode.LeftShift))
{
//rotate around -Z axis
player.transform.Rotate(0, 0, -turnSpeeed * Time.deltaTime, Space.Self);
}
if (Input.GetKey(KeyCode.Space))
{
//rotate around +Z axis
player.transform.Rotate(0, 0, turnSpeeed * Time.deltaTime, Space.Self);
}
}
//MOVEMENT STABILOZATION
public void Stabilaze()
{
Quaternion currentRotation = player.transform.rotation;
Quaternion smoothRotation = Quaternion.Lerp(currentRotation, starterPos.rotation, smoothAmountRotation);
player.rotation = smoothRotation;
}
}
Comment
Your answer
Follow this Question
Related Questions
Swipe Power Limit in a ball 0 Answers
Control accurate velocity and angular velocity 0 Answers
Object sticks to the side of the collider 1 Answer
Roll a Ball Tutorial error 1 Answer