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 tsdavs · Jan 03, 2016 at 09:04 AM · vehicleturninghorizontalclamped rotationlean

Hover car lean limiting turning angle

Hello, what I want to achieve is to have my hover car lean when it turns so it looks natural.

The problem at the moment, the leaning works and the turning works however, only independently of one another. I assume the clamp angle is where I'm going wrong. Not only clamping the lean angle but the turn angle. I assume this because when both scripts are active, I can lean side to side (the angle clamp works) and move forwards/backwards.

This is the leaning script:

     public float MaxTiltAngle = 20.0f;    // The maximum angle the board can tilt
     public float tiltSpeed = 30.0f;       // tilting speed in degrees/second
     Vector3 curRot;
     float maxZ;
     float minZ;
 
     void Start()
     {
         // Get initial rotation
         curRot = this.transform.eulerAngles;
         // calculate limit angles:
         maxZ = curRot.z + MaxTiltAngle;
         minZ = curRot.z - MaxTiltAngle;
     }
 
     void Update()
     {
         // "rotate" the angles mathematically:
         curRot.z += -Input.GetAxis("Horizontal") * Time.deltaTime * tiltSpeed;
         // Restrict rotation along z axes to the limit angles:
         curRot.z = Mathf.Clamp(curRot.z, minZ, maxZ);
 
         // Set the object rotation
         this.transform.eulerAngles = curRot;
     }

This is the "hovering" and moving script:

 Rigidbody m_body;
 float m_deadZone = 0.1f; 
 public float m_hoverForce = 9.0f;
 public float m_hoverHeight = 2.0f;
 public GameObject[] m_hoverPoints;
 
 public float m_forwardAcl = 100.0f;
 public float m_backwardAcl = 25.0f;
 float m_currThrust = 0.0f;
 
 public float m_turnStrength = 10f;
 float m_currTurn = 0.0f;
 
 public GameObject m_leftAirBrake;
 public GameObject m_rightAirBrake;
 
 int m_layerMask;
 
 void Start()
 {
     m_body = GetComponent<Rigidbody>();
 
     m_layerMask = 1 << LayerMask.NameToLayer("Characters");
     m_layerMask = ~m_layerMask;
 }
 
 void OnDrawGizmos()
 {
 
     //  Hover Force
     RaycastHit hit;
     for (int i = 0; i < m_hoverPoints.Length; i++)
     {
         var hoverPoint = m_hoverPoints[i];
         if (Physics.Raycast(hoverPoint.transform.position,
                             -Vector3.up, out hit,
                             m_hoverHeight,
                             m_layerMask))
         {
             Gizmos.color = Color.blue;
             Gizmos.DrawLine(hoverPoint.transform.position, hit.point);
             Gizmos.DrawSphere(hit.point, 0.5f);
         }
         else
         {
             Gizmos.color = Color.red;
             Gizmos.DrawLine(hoverPoint.transform.position,
                            hoverPoint.transform.position - Vector3.up * m_hoverHeight);
         }
     }
 }
 
 void Update()
 {
 
     // Main Thrust
     m_currThrust = 0.0f;
     float aclAxis = Input.GetAxis("Vertical");
     if (aclAxis > m_deadZone)
         m_currThrust = aclAxis * m_forwardAcl;
     else if (aclAxis < -m_deadZone)
         m_currThrust = aclAxis * m_backwardAcl;
 
     // Turning
     m_currTurn = 0.0f;
     float turnAxis = Input.GetAxis("Horizontal");
     if (Mathf.Abs(turnAxis) > m_deadZone)
         m_currTurn = turnAxis;
 }
 
 void FixedUpdate()
 {
 
     //  Hover Force
     RaycastHit hit;
     for (int i = 0; i < m_hoverPoints.Length; i++)
     {
         var hoverPoint = m_hoverPoints[i];
         if (Physics.Raycast(hoverPoint.transform.position,
                             -Vector3.up, out hit,
                             m_hoverHeight,
                             m_layerMask))
             m_body.AddForceAtPosition(Vector3.up
               * m_hoverForce
               * (1.0f - (hit.distance / m_hoverHeight)),
                                       hoverPoint.transform.position);
         else
         {
             if (transform.position.y > hoverPoint.transform.position.y)
                 m_body.AddForceAtPosition(
                   hoverPoint.transform.up * m_hoverForce,
                   hoverPoint.transform.position);
             else
                 m_body.AddForceAtPosition(
                   hoverPoint.transform.up * -m_hoverForce,
                   hoverPoint.transform.position);
         }
     }
 
     // Forward
     if (Mathf.Abs(m_currThrust) > 0)
         m_body.AddForce(transform.forward * m_currThrust);
 
     // Turn
     if (m_currTurn > 0)
     {
         m_body.AddRelativeTorque(Vector3.up * m_currTurn * m_turnStrength);
     }
     else if (m_currTurn < 0)
     {
         m_body.AddRelativeTorque(Vector3.up * m_currTurn * m_turnStrength);
     }
 }


Help or guidance would be greatly appreciated.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tsdavs · Sep 09, 2016 at 01:31 AM

Sorry no the project came to a stop a while ago but I might be picking it up again soon. If you come to an answer please let me know though.

Comment
Add comment · Show 1 · 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 Sneer1 · Sep 09, 2016 at 10:11 AM 0
Share

This version of your script works. I did not use it, cause it does some weird movements to the rigidbody. It does need further tweaks, but it's a start.

 using UnityEngine;
 using System.Collections;
 
 public class Hover_lean : $$anonymous$$onoBehaviour {
 
     public float $$anonymous$$axTiltAngle = 20.0f;    // The maximum angle the board can tilt
     public float tiltSpeed = 30.0f;       // tilting speed in degrees/second
     Vector3 curRot;
     float maxX;
     float $$anonymous$$X;
 
     //void Start()
 
     void Update()
     {
         {
             // Get initial rotation
             curRot = this.transform.eulerAngles;
             // calculate limit angles:
             maxX = curRot.x + $$anonymous$$axTiltAngle;
             $$anonymous$$X = curRot.x - $$anonymous$$axTiltAngle;
         }
     }
 
     void FixedUpdate()
     {
         // "rotate" the angles mathematically:
         curRot.x += -Input.GetAxis("Tilt") * Time.deltaTime * tiltSpeed;
         // Restrict rotation along x axes to the limit angles:
         curRot.x = $$anonymous$$athf.Clamp(curRot.x, $$anonymous$$X, maxX);
 
         // Set the object rotation
         this.transform.eulerAngles = curRot;
     }
 }

avatar image
0

Answer by Sneer1 · Sep 08, 2016 at 10:17 PM

I'm at the same point. Did you found a solution already ?

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Vehicle throttle cut with negative forward velocity 0 Answers

How Would i add animation to this script for vertical and horizontal movement :/ im very new to this and i have no clue 0 Answers

After adding a force to a moving gameobject it will behaviour in a strange way. 0 Answers

Object don't rotate correctly 1 Answer

Need help with nitro boost for car 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