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 Doctor06 · Oct 31, 2014 at 07:45 PM · phonetiltmathf.clamptilting

Using Mathf.Clamp in unity for boundries

I am making a 2D space shooter mobile game. I am trying to set a boundary so the ship does not go out of the screen. You can move the ship by tilting your phone. I have a shipController.cs attached to my ship object. And this is my FixedUpdate function:

 public float xMin, xMax, yMin, yMax;
 void FixedUpdate()
 {
     //tilt to move ship
     transform.Translate(Input.acceleration.x * speed, Input.acceleration.y * speed, 0);

     //create boundries
     rigidbody2D.position = new Vector2(
         Mathf.Clamp(rigidbody2D.position.x, xMin, xMax), 
         Mathf.Clamp(rigidbody2D.position.y, yMin, yMax)
     );
 }

The min and max values are defined in unity inspector. This code works great if you are testing on pc. But when exporting to the phone, the movement is very jittery and glitches. The ship will get to the boundary and kind of start jumping. The movement when tilting is also not very smooth. Is there any other way to make this smoother?

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 Habitablaba · Oct 31, 2014 at 07:51 PM 0
Share

I wonder if this isn't an issue with the speed the ship is moving when it hits the bounds. Or rather, an issue with the amount of acceleration you're trying to put on it when it is at the boundary.
If you tilt the phone ever so slightly, does it still bounce?

avatar image Doctor06 · Oct 31, 2014 at 08:40 PM 0
Share

yes. The more i tilt, the bigger the bounce. But if i tilt a little, it still bounces a little.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by JonS · Oct 31, 2014 at 08:36 PM

Typically Physics objects don't like being moved around manually very much. FixedUpdate is for Physics stuff so if you want to use that you should be using rigidbody2D.AddForce() instead of Translate and position. Then you'd clamp the range and set the rigidbody's velocity to 0 if he was trying to move off the screen.

Alternatively, you can set the rigidbody to be Kinematic and move things in Update using what you have here if you don't really need the ship to be pushing things around with physics.

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 SheZii · Feb 07, 2019 at 08:44 AM

If you want to clamp/restrict gameobjects on the boundary of Minimap, here are the links


Youtube: https://www.youtube.com/watch?v=xW4zZQhRKlI
Step by step tutorial: http://techscenarios.com/unity3d-clamp-objects-on-edges-of-minimap-clamp-on-minimap-boundary-free-scripts-part-4/

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Tilt an object when rotating and set it back to normal afterwards 0 Answers

How to get moving player to tilt when turning 0 Answers

Tilt Function. HELP! 1 Answer

gui and tilt function 1 Answer

My vehicle wont perform both the Tilt and Turn 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