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 RobbyT15 · May 15, 2016 at 04:07 PM · rotatemathf.clamp

How do I allow my player object to continue rotating once it's met a clamped boundary?

I have a spaceship I'm moving around on the screen, when the player moves the ship, it rotates on its axis. However, when the ship meets a clamped boundary(on the x-axis) it abruptly stops. How do I make it so that the ship will still rotate when meeting the clamped boundary?

 void Controls(){
     Rotate();
     MoveShip();
 }
 void Rotate(){
     player.rotation = Quaternion.Euler((player.velocity.y * -pitchAngle), 0f, (player.velocity.x * -bankRotation));
 }
 void MoveShip(){
     float moveVertical = Input.GetAxis("Vertical");
     float moveHorizontal = Input.GetAxis("Horizontal");
     Vector3 bank = new Vector3(moveHorizontal, 0f, 0f);
     Vector3 pitch = new Vector3(0f, moveVertical, 0f);
     player.AddForce(bank * bankSpeed);
     player.AddForce(pitch * pitchSpeed);
     player.position = new Vector3(Mathf.Clamp(player.position.x, xMin, xMax), Mathf.Clamp(player.position.y, yMin, yMax), 0f);
     if((player.position.x == xMin) || (player.position.x == xMax) || (player.position.y == yMin) || (player.position.y == yMax)){
         player.velocity = new Vector3(0f, 0f, 0f);
         player.angularVelocity = new Vector3(0f, 0f, 0f);
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eno-Khaon · May 15, 2016 at 06:03 PM

First, there's your Rotate() function:

 void Rotate(){
     player.rotation = Quaternion.Euler((player.velocity.y * -pitchAngle), 0f, (player.velocity.x * -bankRotation));
 }

Your rotational control is tied to your current velocity. You could apply a minimum potential multiplier to it in order to prevent complete immobility if desired:

 public float minMult; // minimum velocity multiplier
 
 // ...
 
 player.rotation = Quaternion.Euler((Mathf.Max(player.velocity.y, minMult) * -pitchAngle), 0f, (Mathf.Max(player.velocity.x, minMult) * -bankRotation));


Next, there's the physics-based aspect.

When you reach any boundary, all motion is immediately and entirely halted for both player.velocity and player.angularVelocity. First, you clamp the position. Then, if the position is at a boundary edge, you stop every frame.

Because all motion is stopped every frame upon reaching a boundary edge, no new motion can be applied. Depending on the intended behavior, the solution may be as simple as clamping position without stopping motion, or it may be more complicated, such as determining whether the player is moving towards or away from each boundary.

Comment
Add comment · Show 3 · 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 RobbyT15 · May 15, 2016 at 06:14 PM 0
Share

I'm assu$$anonymous$$g clamping the position without stopping the motion would cause an issue such as continuing to move outside of the designated boundary?

If I went with using a $$anonymous$$imal potential multiplier, would it be safe to assume that it would need to be the same as my bank speed so it would continue to rotate at the same speed?

avatar image Eno-Khaon RobbyT15 · May 15, 2016 at 09:02 PM 0
Share

If you only clamp the position every frame, then you'll still have velocity trying to pull you beyond the boundaries, though you shouldn't be moving further.

At the moment, your rotational speed scales up with your current velocity. With that in $$anonymous$$d, the $$anonymous$$imum would simply act as a safety to permit rotation at all while you're not moving.

avatar image RobbyT15 Eno-Khaon · May 16, 2016 at 07:13 PM 0
Share

That was an issue I was having was when I called the $$anonymous$$oveShip function in FixedUpdate, the ship would still rotate when it hit be clamped positions, but would actually continue to move passed them as well at a very slow pace. I'm guessing was happening because the velocity was scaled up and still had a value greater than zero since I was holding the button down.

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

vector3 rotatetowards don't work 1 Answer

angle add constant at vector3 1 Answer

Roatate object to player on tag 0 Answers

How to Rotate on Y Axis by 90 degrees left or right? 1 Answer

it is possible to rotate a texture ? 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