How can I make my airplane tilt?
I have an airplane and I am trying to make it tilt when I turn left right, up down, however, I don't know how to do that, and I have looked around quite a bit, but nothing has worked.
Here is what I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
void Update ()
{
Rigidbody rb = GetComponent<Rigidbody>();
if (Input.GetKey(KeyCode.A)) rb.AddForce(Vector3.right);
if (Input.GetKey(KeyCode.D)) rb.AddForce(Vector3.left);
if (Input.GetKey(KeyCode.W)) rb.AddForce(Vector3.back);
if (Input.GetKey(KeyCode.S)) rb.AddForce(Vector3.forward);
if (Input.GetKey(KeyCode.Space)) rb.AddForce(Vector3.up);
if (Input.GetKey(KeyCode.LeftShift)) rb.AddForce(Vector3.down);
}
}
If I remember my physics 101 right then object will never rotate when force applied acts on it's center of mass ( rb.AddForce
). What you need is rb.AddForceAtPosition or rb.AddTorque.
Answer by Spudly1701 · Feb 04 at 06:20 PM
A bit rusty with Unity, but I think you'll need to create a new float variable called 'force' or something, and then in your update, use AddTorque instead of AddForce (at the moment I assume that your plane slides rather than turn).
if (Input.GetKey(KeyCode.A)) rb.AddTorque(-force); if (Input.GetKey(KeyCode.D)) rb.AddTorque(force);
I think anyway.
Spud
Your answer
Follow this Question
Related Questions
Unity standard assets 2018.4 Aircraft Ai isn't working on my own 3d airplane model. 0 Answers
Unwanted up/down (pitch) movement when tilting 0 Answers
How do I tilt my Spaceship and make it go up and down? 2 Answers
Problem titling an object from its center (Pitch/Roll) (screenshot) 0 Answers
How to avoid distortion of Light2D on the huge Sprites? 0 Answers