Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Light_ · Jul 29, 2016 at 05:21 PM · rotationridigbody

change the forward, or maybe normalize forward, depending on rotation

I'm prototyping a skateboarding game so I need the board to continue to travel quite a distance after the force has been applied so I've got friction and mass down and using

     public float moveSpeed;                    // forward speed
     public float rotateSpeed;                // speed when turning
     public float jumpSpeed;                    // force applied to a jump
     public Rigidbody boardbody;             // call the rigidbody
     private bool isGrounded;                // raycast to the ground
     private bool isNotGrounded;                // raycast to the ground
     private Quaternion xrotation;            // trying to normalize forward
     private Quaternion rotCur;                // Quat for alignment

          void Move()
     {
         // forward
         if (Input.GetButton ("A") && isGrounded)
             boardbody.AddForce (transform.forward * moveSpeed);
         // slowdown
         if (Input.GetButton ("B") && isGrounded)
             boardbody.AddForce (-transform.forward * (moveSpeed / 2));
     }

to go forward and slowdown which is working nicely. I've got a few rotation things to tilt and to properly rotate, for example my rotation >>

     void Rotate()
     {
         if (isGrounded)
         {
             float rotateAroundX = Input.GetAxis ("LeftStickX") * rotateSpeed;
             Quaternion xrotation = Quaternion.Euler (0, rotateAroundX, 0);
             boardbody.MoveRotation (boardbody.rotation * xrotation);
         }
     }

However, after I stop applying forward motion with ("A") the rotation no longer effects the "forward" or rather I should say if not holding down ("A") then the board just spins (rotates correctly lol) and continues traveling in whatever direction.

I'd like the board to change its "forward" (I think that verbalizes what I'm aiming for) while continuing to coast or "roll" forward even if I'm not holding down the ("A")

I started another method to try to maybe normalize the forward or update it I'm not sure what I need to do but his is where I am at

 void FixForward()    // set apply forward magnitude to new rotation angle
     {
         if (isGrounded)
         {
             Vector3 targetForward = xrotation * Vector3.forward;
 
         }
     }

because a Quaternion isn't really a vector right so I tried to define one. I am just not sure how to apply it, when I do something like >boardbody.transform.rotation = targetForward * moveSpeed;< I get a constant force that pushes the board ha ha and it flies off the world.

I'm sure it has to do with the fact that I'm applying forces to transform rather than world space V3, Anyway if you guys have any ideas I really appreciate it!

Comment
Add comment · Show 4
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 Light_ · Jul 30, 2016 at 05:22 PM 0
Share

So i tried changing the way the I added force to the rigidbody forward, so ins$$anonymous$$d of boardbody(rigidbody).addforce I tried boardbody.veloctiy, and addrelativevelocity, and after I got each try working in a different way, the problem with turning persisted. I assume then I need to change the rotation to something else, or figure out how to update the forward position with each turn?

avatar image Light_ · Jul 30, 2016 at 06:25 PM 0
Share

So using this guide >> link text

it said not to apply a Quaternian to a rigidbody so ins$$anonymous$$d I tried this bit of code >>

 float rotateAroundX = Input.GetAxis ("LeftStickX");
     boardbody.AddTorque(transform.up * rotateSpeed * rotateAroundX);

nonetheless the object rotates but does not carry momentum into its forward facing position.

avatar image Light_ · Jul 30, 2016 at 07:19 PM 0
Share

Tried this, I think I'm getting close, well I hope so,

     void FixForward()    // set apply forward magnitude to new rotation angle
     {
         
         if (isGrounded)
             {
             Vector3 direction = boardbody.transform.position - transform.position;
             boardbody.AddForceAtPosition (direction.normalized, transform.position);
             }
             
     }

I put in AddForceAtPosition like the docs recomend, though it hasn't had an effect I hope this is what I need!

avatar image Light_ · Jul 30, 2016 at 08:02 PM 0
Share

$$anonymous$$aybe I'm going about this all wrong, I have very little mass and very very little friction trying to get the player to roll for a long distance, perhaps this is why when turning it just spins? How can I get the player to keep moving forward after applying the force, and either turn up friction or mass or both I wonder?

1 Reply

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

Answer by Light_ · Jul 31, 2016 at 08:26 PM

Well I have a viable solution, and after almost a week of working on this one problem I'm pretty happy to move on, I added this method:

 void FIxForward()    // set apply forward magnitude to new rotation angle
     {
         if (boardbody.velocity.magnitude < 3.0f) {
             isMoving = false;
         }
         else if (boardbody.velocity.magnitude > 3.0f) {
             isMoving = true;
         }
         if (isMoving && isTurning) {
             boardbody.AddForce (transform.forward * (moveSpeed));
         } else if (isMoving && !isTurning) {
             boardbody.AddForce (transform.forward * 0.0f);
         }
     }

I'll add a timer or something so eventually the board comes to a stop, but YAY it turns like its supposed to!!

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 Bunny83 · Jul 31, 2016 at 10:59 PM 1
Share

Just want to add your if statement in line 6 is unnecessary since it's the reverse of the statement in line 3. Also at the moment you have a "hole" in the velocity range at "3.0f". It's very unlikely that this ever happens but it's still a logic error.

Next thing is if you just want to enable a boolean value when a condition is true and disable it when it's false, you don't need an if statement at all. The condition inside your if statement is already a boolean expression. You can simply "store" it in your boolean:

 is$$anonymous$$oving = boardbody.velocity.magnitude > 3.0f;

This will make "is$$anonymous$$oving" true when the velocity is greater than 3, otherwise it's false.

Next thing is this line:

 boardbody.AddForce (transform.forward * 0.0f);

is completely pointless ^^. If you multiply a vector by 0.0f it becomes Vector3.zero which is (0, 0, 0). Adding zero force is pointless so you can remove the whole line including the if statement.

So this would do the same:

 is$$anonymous$$oving = boardbody.velocity.magnitude > 3.0f;
 if (is$$anonymous$$oving && isTurning) {
     boardbody.AddForce (transform.forward * (moveSpeed));
 }

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

62 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

Related Questions

Wrong contineous rotation to car 0 Answers

Flip over an object (smooth transition) 3 Answers

Affine transformations to Object 3 Answers

Rotation Problem 1 Answer

Quaternion Rotation On Specific Axis Issue 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