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 /
avatar image
0
Question by Lairinus · May 13, 2013 at 04:02 PM · c#rotationtransformlocaleulerangles

Maintaining direction while jumping / rotating

Hello,

I have three Vector3s: movementVector, jumpVector, fallVector.

I want to be able to jump forward and rotate my object without its direction changing. I just want to make a 180 and still head in the direction without always respecting the localEulerAngles. (My camera script changes my object's look angle to match the Camera's)

  • movementVector converts input to the object's local euler angles. It works fine.

  • jumpVector is set to movementVector at the time of jumping. This value never changes until a new jump is made.

  • fallVector is set the second the object isn't grounded / the jump ends.

AddForce will not work (at least not with my methodology), as I have incremental Gravity and if I AddForce more than once (to update the gravity) I get thrown outside of the map. At that, I tried some things with Lerp with low success mainly because it also doesn't seem to respect the gravity.

Any suggestions?

Comment
Add comment · Show 2
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 bodec · May 13, 2013 at 04:21 PM 0
Share

can I assume right you are using a rigidbody for movement?

avatar image Lairinus · May 13, 2013 at 04:30 PM 0
Share

I have a rigidbody attached to the character solely for collision extrapolation. I'm using Translate() for every other aspect of movement aside from the "falling" and "jumping."

Because of the rigidbody I tried using AddForce.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Lairinus · May 14, 2013 at 04:31 AM

12 hours later and I found a solution using transform.Translate().

My full code is :

     private bool towardAerialAngles = false;
     void translateUnit()
     {
         //Figure out how to jump in one direction and continue going in one direction
         if (gravity.isGrounded && !gravity.isJumping) //If the unit is grounded
         {
             Ray ray = new Ray(transform.position,transform.forward); //Make a ray shoot foward **Change Eventually**
             RaycastHit hit;
             if (!Physics.Raycast (ray, out hit, _groundedRayDistance)) //Cast the ray in the movement direction
             {    
                 towardAerialAngles = false;
                 _gravityDisplacement = _movementVector;
                 parent.parentObject.transform.Translate(gravity.ActualGravity(_movementVector) * 2); //Move if there wasn't a hit
             }
         }
         else if (!gravity.isGrounded)
         {
             if (gravity.isJumping)
             {
                 if (!towardAerialAngles)
                 {
                     towardAerialAngles = true;
                     jumpDirection = parent.parentObject.transform.TransformDirection(jumpDirection + parent.parentObject.transform.localEulerAngles);
                 }
                     parent.parentObject.transform.Translate(gravity.ActualGravity(jumpDirection), Space.World);
             }
             else
             {
                 if (!towardAerialAngles)
                 {
                     towardAerialAngles = true;
                     jumpDirection = parent.parentObject.transform.TransformDirection(jumpDirection + parent.parentObject.transform.localEulerAngles);
                 }
                     parent.parentObject.transform.Translate(gravity.ActualGravity(jumpDirection), Space.World);
             }
         }
         else
         {
             towardAerialAngles = false;
             parent.parentObject.transform.Translate(gravity.ActualGravity(_movementVector));
         }
     }

Specifically here :

 jumpDirection = parent.parentObject.transform.TransformDirection(jumpDirection + parent.parentObject.transform.localEulerAngles);


and here :

 parent.parentObject.transform.Translate(gravity.ActualGravity(jumpDirection));
 

Same concept as stopping an IEnumerator with the boolean, except I call it once when the unit isn't grounded. Then when the unit is grounded, I set it to true.

jumpDirection is set in a different script the second the unit isn't grounded.

Hope this helps out anyone with similar answers... sure spent long enough on it lol.

Also, ugly as hell and not commented at this point, too excited.

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
avatar image
0

Answer by bodec · May 13, 2013 at 04:31 PM

if you set up your forward movement in a statement of if grounded that should solve your being able to add force while in the air stopping you from moving forward or backward.

Comment
Add comment · Show 8 · 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 Lairinus · May 13, 2013 at 05:08 PM 0
Share

I'll try that.. I already had something like that before but I'll give it another attempt

avatar image Lairinus · May 13, 2013 at 07:07 PM 0
Share

In short, it still semi-works.

$$anonymous$$y code is :

                 rigidbody$$anonymous$$anager.SetAndClearDrag(true);
                 parent.parentObject.rigidbody.AddForce(gravity.ActualGravity(fallingDirection));

the ActualGravity method returns the movement speed and gravity and multiplies it by the vector.

SetAndClearDrag is implemented in order to... set and clear the drag of the rigidbody so that it can move freely in air and be clamped when on the ground. I use is$$anonymous$$inematic as well to reset the force..

One question, though. (I'll post the method as well)

When I jump / fall I only do so in one direction. Despite adding/subtracting the position, it functions the same. (Aka, fallingDirection +/- transform.position)

$$anonymous$$ethod:

     public Vector3 ActualGravity(Vector3 movementVector)
     {
         Vector3 _gravity = movementVector;
         _gravity.x *= 5; //$$anonymous$$ultiply by movement speed
         _gravity.y = gravity + manageUpwardForce();
         _gravity.z *= 5; //$$anonymous$$ultiply by movement speed
         return _gravity;
     }

Any suggestion on how to have it move in the current direction?

avatar image bodec · May 13, 2013 at 07:56 PM 0
Share

When I get home I'll post a script for you to look at might help

avatar image bodec · May 13, 2013 at 09:06 PM 0
Share

rereading this code on a screen larger than my phone I see the code I was going to toss up wont help as its a rigidbody fps controller is this a game like qubert or are you tossing squares?

avatar image Lairinus · May 13, 2013 at 09:29 PM 0
Share

I'm using the code as a part of my main character.

I have gravity and movement direction figured out, I just want to jump and rotate while jumping without changing direction

Show more comments
avatar image
0

Answer by ABEMOS · Dec 09, 2013 at 05:18 AM

I am relatively new to unity, but have you tried adding " Space.World" to your transform.Translate? this should make the movement continue to happen in the absolute set direction regardless of the new rotation of the object. e.g. (from the scripting reference):

transform.Translate(Vector3.up * Time.deltaTime, Space.World);

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

16 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

Related Questions

Flip over an object (smooth transition) 3 Answers

How to rotate a projectile to face the player in 2D? 1 Answer

C# Mathf.PingPong Rotate Back and Forth 2 Answers

Saving coordinates and transferring them to the second object 1 Answer

Wheels Rotate or Turn, but not both 1 Answer


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