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 /
  • Help Room /
avatar image
0
Question by Squalol · Sep 02, 2015 at 01:54 AM · physicsrigidbodyrigidbody.addforcerigidbody physics

Conserve some of Rigidbody's speed when turning.

Hello all, Im trying to make a top down spaceship game. My code is a slightly modified version of this code: http://wiki.unity3d.com/index.php?title=ShipControls . The problem im having is that it works wonderfully with small ships like fighthers since they have a lot of thrust so they can change direction fast.

However i want bigger and more heavy ships to gradually accelerate and take them several seconds until they reach max speed. The problem comes when i have to turn with them, they just keep drifting in the previous direction way too long. Ideally I'd like this big ships to behave more like actual sea ships than spaceships where if they turn, they loose some speed but keep most of it.

Im not very experienced with physics so i have no idea how to handle it. If there is any way to get a similar result to what im doing WITHOUT physics I'd be very grateful since as far as i know, physics are very bad for smartphones right?

This is the code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 // Put this on a rigidbody object and instantly
 // have 2D spaceship controls like OverWhelmed Arena
 // that you can tweak to your heart's content.
 
 [RequireComponent (typeof (Rigidbody))]
 public class ShipControls : MonoBehaviour
 {
     public float hoverHeight = 3F;
     public float hoverHeightStrictness = 1F;
     public float forwardThrust = 5000F;
     public float backwardThrust = 2500F;
     public float bankAmount = 0.1F;
     public float bankSpeed = 0.2F;
     public Vector3 bankAxis = new Vector3(-1F, 0F, 0F);
     public float turnSpeed = 8000F; 
 
 
     public float maxSpeed;
     public Text textoMotores;
     public Slider sliderMotores;
     public Text textoSpeed;
     
 
     
     public Vector3 forwardDirection = new Vector3(1F, 0F, 0F);
     
     public float mass = 5F;
     
     // positional drag
     public float sqrdSpeedThresholdForDrag = 25F; //VELOCIDAD A LA QUE SE EMPEZARA A APLICAR EL FAST DRAG PARA
     //PONER UNA VELOC MAX
     public float superDrag = 2F;   //DRAG CUANDO NO SE PULSA NADA
     public float fastDrag = 0.5F;
     public float slowDrag = 0.01F;
     
     // angular drag
     public float sqrdAngularSpeedThresholdForDrag = 5F;  //VELOCIDAD DE GIRO A LA QUE SE ACTIVARA FAST DRAG
     public float superADrag = 32F;
     public float fastADrag = 16F;
     public float slowADrag = 0.1F;
     
     public bool playerControl = true;
     
     float bank = 0F;
     
     void SetPlayerControl(bool control)
     {
         playerControl = control;
     }
     
     void Start()
     {
         GetComponent<Rigidbody>().mass = mass;
     }
     
     void FixedUpdate()
     {
         textoSpeed.text = GetComponent<Rigidbody> ().velocity.magnitude.ToString ();
         sqrdSpeedThresholdForDrag = thrust * maxSpeed;
         if (Mathf.Abs(thrust) > 0.01F)
         {
             if (GetComponent<Rigidbody>().velocity.magnitude > sqrdSpeedThresholdForDrag)
                 GetComponent<Rigidbody>().drag = fastDrag;
             else
                 GetComponent<Rigidbody>().drag = slowDrag;
         }
         else
             GetComponent<Rigidbody>().drag = superDrag;
         
         if (Mathf.Abs(turn) > 0.01F)
         {
             if (GetComponent<Rigidbody>().angularVelocity.sqrMagnitude > sqrdAngularSpeedThresholdForDrag)
                 GetComponent<Rigidbody>().angularDrag = fastADrag;
             else
                 GetComponent<Rigidbody>().angularDrag = slowADrag;
         }
         else
             GetComponent<Rigidbody>().angularDrag = superADrag;
         
         transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, hoverHeight, transform.position.z), hoverHeightStrictness);
         
         float amountToBank = GetComponent<Rigidbody>().angularVelocity.y * bankAmount;
         
         bank = Mathf.Lerp(bank, amountToBank, bankSpeed);
         
         Vector3 rotation = transform.rotation.eulerAngles;
         rotation *= Mathf.Deg2Rad;
         rotation.x = 0F;
         rotation.z = 0F;
         rotation += bankAxis * bank;
         //transform.rotation = Quaternion.EulerAngles(rotation);
         rotation *= Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(rotation);
 
     }
     
     float thrust = 0F;
     float turn = 0F;
     
     void Thrust(float t)
     {
         thrust = Mathf.Clamp(t, -1F, 1F);
     }
     
     void Turn(float t)
     {
         turn = Mathf.Clamp(t, -1F, 1F) * turnSpeed;
     }
     
     bool thrustGlowOn = false;
     
     void Update ()
     {
         Debug.Log (this.gameObject.name + GetComponent<Rigidbody> ().velocity.magnitude);
         float theThrust = thrust;
         
         if (playerControl)
         {
             if (Input.GetKey (KeyCode.W)) {
                 thrust +=0.1f;
                 if (thrust>1){
                     thrust = 1;
                 }
             }
             
             if (Input.GetKey (KeyCode.S)) {
                 thrust -=0.1f;
                 if (thrust<-1){
                     thrust = -1;
                 }
             }
 
             if (Input.GetKey (KeyCode.X)) {
                 thrust =0f;
                 }
             
             sliderMotores.value = thrust * 100;
             textoMotores.text = (Mathf.Round(thrust * 100)).ToString();
             turn = Input.GetAxis("Horizontal") * turnSpeed;
         }
         
         if (thrust > 0F)
         {
             theThrust *= forwardThrust;
             if (!thrustGlowOn)
             {
                 thrustGlowOn = !thrustGlowOn;
                 BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver);
             }
         }
         else
         {
             theThrust *= backwardThrust;
             if (thrustGlowOn)
             {
                 thrustGlowOn = !thrustGlowOn;
                 BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver);
             }
         }
         
         GetComponent<Rigidbody>().AddRelativeTorque(Vector3.up * turn * Time.deltaTime);
 
         GetComponent<Rigidbody>().AddRelativeForce(forwardDirection * theThrust * Time.deltaTime);
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

28 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

Related Questions

How to move a kinematic rigidbody with touch? 0 Answers

Counteracting rigidbody velocity using AddForce? 0 Answers

Rigidbody Addforce Not Working 1 Answer

Player (RigidBody) jitters when colliding with object and jumping. 0 Answers

Rigid body robot animation 0 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