Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by 017Bluefield · Sep 14, 2019 at 01:19 AM · scripting problemphysicsrigidbodycontrolturning

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);
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 017Bluefield · Sep 19, 2019 at 02:59 AM 0
Share

I'll give this a try and get back to you on that.

avatar image 017Bluefield · Sep 19, 2019 at 03:25 AM 0
Share

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.

avatar image 017Bluefield · Sep 19, 2019 at 03:29 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

267 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

transform.forward not always forward? 2 Answers

Futurama Cars 1 Answer

Turning a rigidbody that's in motion 2 Answers

How should I control a 'rake' that pushes objects around a table? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges