- Home /
Flying Rigidbody moves forward, but doesn't turn in mid-flight?
I'm working on a 3D flying game. I've managed to get the player Rigidbody to move forward automatically, but now I'm working on getting the Rigidbody to turn and change its flight. So far I've only managed to get the model and camera to look around, either with the A and D keys or with a mouse. The problem is that it's not actually changing the Rigidbody's flight path at all; it simply keeps moving forward while ignoring those inputs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragonControllerScript : MonoBehaviour
{
public Camera MainCamera; // Reference to camera
public float forwardPower = 20f; // Dragon's forward movement power/speed
public float turnSpeed = 20f; // Speed of turning (positive = turn to right, negative = turn to left)
private Rigidbody m_Rigidbody; // Rigidbody of the dragon
private Vector3 m_EulerAngleVelocity; // Reference for turning the dragon in midair
private void Start() // Start: called before the first frame update
{
m_EulerAngleVelocity = new Vector3(0, 0, 0);
m_Rigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
m_Rigidbody.AddRelativeForce(Vector3.forward * forwardPower - m_Rigidbody.velocity);
Quaternion deltaRotation = Quaternion.Euler(m_EulerAngleVelocity * Time.deltaTime);
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * deltaRotation);
if (Input.GetKeyDown("A"))
{
m_EulerAngleVelocity = new Vector3(0, -20, 0);
}
if (Input.GetKeyDown("D"))
{
m_EulerAngleVelocity = new Vector3(0, 20, 0);
}
}
}
Answer by Captain_Pineapple · Sep 14, 2019 at 10:25 AM
Hey there,
your problem is that you have the calculation: Vector3.forward * forwardPower-m_Rigidbody.velocity
So as soon as you reach a velocity of forwardPower
there is no possibility to accelerate anymore in any direction. As a possible perhaps working untested solution:
m_Rigidbody.AddRelativeForce(Vector3.forward * forwardPower);
m_Rigidbody.velocity = Vector3.ClampMagnitude(m_Rigidbody.velocity,-maximumSpeed, maximumSpeed);
Let me know if something was unclear.
Ran into a snag: I get compiler errors on maximumSpeed
(which doesn't exist in the script), and this: "Assets\Scripts\DragonControllerScript.cs(24,40): error CS1501: No overload for method 'Clamp$$anonymous$$agnitude' takes 3 arguments"
Here's my script so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragonControllerScript : $$anonymous$$onoBehaviour
{
public Camera $$anonymous$$ainCamera; // Reference to camera
public float forwardPower = 20f; // Dragon's forward movement power/speed
public float turnSpeed = 20f; // Speed of turning (positive = turn to right, negative = turn to left)
private Rigidbody m_Rigidbody; // Rigidbody of the dragon
private float maximumSpeed; // ?????????
private Vector3 m_EulerAngleVelocity; // Reference for turning the dragon in midair
private void Start() // Start: called before the first frame update
{
m_Rigidbody = GetComponent<Rigidbody>();
m_EulerAngleVelocity = new Vector3(0, 0, 0);
}
void FixedUpdate()
{
m_Rigidbody.AddRelativeForce(Vector3.forward * forwardPower);
m_Rigidbody.velocity = Vector3.Clamp$$anonymous$$agnitude(m_Rigidbody.velocity, -maximumSpeed, maximumSpeed);
Quaternion deltaRotation = Quaternion.Euler(m_EulerAngleVelocity * Time.deltaTime);
m_Rigidbody.$$anonymous$$oveRotation(m_Rigidbody.rotation * deltaRotation);
if (Input.Get$$anonymous$$eyDown("A"))
{
m_EulerAngleVelocity = new Vector3(0, -20, 0);
}
if (Input.Get$$anonymous$$eyDown("D"))
{
m_EulerAngleVelocity = new Vector3(0, 20, 0);
}
}
}
Am I doing this right? I feel like I've handled the script wrong.
Plus, with an error-free version of the code (e.g. just m_Rigidbody.AddRelativeForce(Vector3.forward * forwardPower);
), the flyer goes way faster than I need it to.