Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ibwittit · Oct 11, 2021 at 02:33 PM · rotate objectforces

Rotating a Game Object Then Applying Force Towards Rotated Direction

I am trying to mimic the natural flight of a frisbee.

Right now I have three scripts, one applies forward force to the disc, one tilts the disc and another applies force in the tilted direction.

The issue that I am having is that when the player throws the disc from one side of the level it acts as it should (flys forward then eventually fades LEFT and looses altitude). However if I throw the disc from the opposite side of the level, it flys forward but this time tilts RIGHT and applies force in that direction (so the disc tilts right and then looses altitude.

I think my issue is coming in when I am trying to Lerp the disc. My vague comprehension is that the disc is perhaps rotating in world space as opposed to self space, but everything I have tried so far will not make it rotate properly.

ThrowDisc.cs is attached to my player game object. TiltDisc.cs and ApplyTiltForce.cs are attached to my instantiated disc game object.

ThrowDisc.cs

 public class ThrowDisc : MonoBehaviour
 {
     public GameObject discHolster;
     public float throwForce = 40f;
 
     private GameObject cachedDisc;
 
     void Start()
     {        
         discHolster.transform.rotation = default;
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             ThrowRBDisc();
         }
     }
 
     public void SetDisc(GameObject discToAdd)
     {
         cachedDisc = discToAdd;
     }
 
     public void ThrowRBDisc()
     {        
         GameObject disc = Instantiate(cachedDisc, transform.position, transform.rotation);
 
         Rigidbody rb = disc.GetComponent<Rigidbody>();
 
         rb.AddForce(transform.forward * throwForce, ForceMode.VelocityChange);
 
     }
 }
 

TiltDisc.cs

 public class TiltOutDisc : MonoBehaviour
 {
     public float transitionSpeed = 1f;
 
     Vector3 tiltOutVector = new Vector3(0, 0, 45);
 
     private Rigidbody _discRb;
 
 
     void Start()
     {
         _discRb = GetComponent<Rigidbody>();
     }
 
     void Update()
     {
         if (!_didCollide)
         {
             if (_discRb.velocity.magnitude < 40f) TiltOut();
         }
     }
 
     void TiltOut()
     {
         Debug.Log("Tilting Out");
 
         Vector3 currentAngle = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, tiltOutVector.x, Time.deltaTime * transitionSpeed),
         Mathf.LerpAngle(transform.rotation.eulerAngles.y, tiltOutVector.y, Time.deltaTime * transitionSpeed),
         Mathf.LerpAngle(transform.rotation.eulerAngles.z, tiltOutVector.z, Time.deltaTime * transitionSpeed));
 
         transform.eulerAngles = currentAngle;
     }
 }
 

ApplyTiltForce.cs

 public class ApplyTiltForce : MonoBehaviour
 {
 
     private Rigidbody _discRb;
 
     public float divideBy = 6f;
 
     private float _currentZ;
 
     private float _multipliedForce = 0f;
 
     void Start()
     {
         _discRb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         ApplyConditionalAngle();
     }
 
     void ApplyConditionalAngle()
     {
         /// If disc is leaning to the left.
         if (transform.eulerAngles.z >= 1f && transform.eulerAngles.z <= 60f)
         {
             /// The discs z-axis value to _currentZ.
             _currentZ = transform.eulerAngles.z;
 
             /// _currentZ needs to be a lower value so lets divide it before applying
             /// as a force value.
             _currentZ = _currentZ / divideBy;
 
             /// Apply force in the form of the z-axis value to the x-axis of
             /// the disc.  Multiply force if preferred.
             _discRb.AddForce(-_currentZ, 0f, 0f * _multipliedForce);
         }
 
         /// If disc is leaning to the right.
         if (transform.eulerAngles.z <= 360f && transform.eulerAngles.z >= 300f)
         {
             /// Returns a negative value
             _currentZ = transform.eulerAngles.z - 360f;
 
             /// Converts negative value into a positive value.
             _currentZ *= -1f;
 
             /// Lower the value by dividing it.
             _currentZ = _currentZ / divideBy;
 
             /// Apply calculated force to x-axis * _multipliedForce.
             _discRb.AddForce(_currentZ, 0f, 0f * _multipliedForce, ForceMode.Acceleration); // Default
         }
     }
 }
 

Any help would be immensely appreciated, please let me know if I left out any information that might help solve this issue.

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

165 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

Related Questions

Movement issue, the player accelerates but does not reduce in speed when hitting a wall or object 1 Answer

Making object move up and down in Y axis while rotating down and up 1 Answer

Rotate and object with Touch 0 Answers

3D First Person Game, 2D Enemy Face Player/Camera 1 Answer

How come when I rotate my spaceship, it also rotates on the two other axis's? Please help? 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