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 /
avatar image
0
Question by angryFlo · May 05, 2016 at 12:20 PM · androidboundsclampinput.acceleration

clamp input.acclereation within screen width

I'm making a game where you should move your player on an android device by using the input.acclereation functtion. The player moves only on the x.axis, it works good. My problem is, that the player is running out of the screen on left and right side of the x axis. I tried to clamp the movement to the screen width, it works either but not very smoothly. If the player touches the screen border and you want to go to the other side it jumps from the wall instead from moving to the other site.

Here ist my code:

 float targetSpeed;
 targetSpeed = Mathf.Clamp (Input.acceleration.x, -0.25f, 0.25f);
 
 Vector3 ObjectScreenPosition = Camera.main.WorldToScreenPoint (transform.position);
 float maxwidthRight = (Screen.width - 30);
 float maxwidthLeft = (Screen.width - Screen.width + 57);
 
 if (ObjectScreenPosition.x >= maxwidthRight) {
     targetSpeed = Mathf.Clamp (Input.acceleration.x, -0.2f, 0f);
       } else if (ObjectScreenPosition.x <= maxwidthLeft) {
      targetSpeed = Mathf.Clamp (Input.acceleration.x, -0f, 0.2f);
 }
                 
 transform.Translate (targetSpeed * speed * 145, transform.position.y, transform.position.z);
             

Do you have any sugestions for me to do it in a better way ? Or an idea to fix my "bounce" problem ? Thanks !

Comment
Add comment · Show 3
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 FortisVenaliter · May 05, 2016 at 03:17 PM 0
Share

Well, first off, your line

 float maxwidthLeft = (Screen.width - Screen.width + 57);

can be simplified with

 float maxwidthLeft = 57;

Because (Screen.width-Screen.width) = 0

Beyond that though, your code largely makes sense and should work... Can you maybe post a video of what is happening so we can get a better idea?

avatar image angryFlo FortisVenaliter · May 05, 2016 at 05:24 PM 0
Share

Thanks, i changed the code for the width = 57 ;-) I'll try to do a video an will upload it soon.

avatar image fabian-mkv · May 05, 2016 at 04:01 PM 0
Share

Not sure if this is related to your problem, but you probably don't want to use the transform.position value in transform.Translate in your code:

 transform.Translate (targetSpeed * speed * 145, transform.position.y, transform.position.z);

$$anonymous$$aybe you meant to do this:

 transform.Translate (targetSpeed * speed * 145, 0, 0);

2 Replies

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

Answer by angryFlo · May 05, 2016 at 06:50 PM

The Problem is solved, i changed three things. 1. Changeds the suggestions from FortisVenaliter 2. Changeds the suggestions from fabian.mkv 3. I added this code in the start section of my script: transform.position = new Vector3 (0, 0, 0);

Maybe one of these or alle three together fixes the problem, thanks for help.

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 angryFlo · May 05, 2016 at 05:23 PM

Thanks for the suggestions i'll figure out your code and will give an Update.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Who to limit Sprite with tilt control, to screen boundaries? 0 Answers

Calculating linear acceleration of a phone on Android 1 Answer

How to calibrate accelerometer according to start ? 1 Answer

Clamp player to screen borders 1 Answer

Prevent Object from going out of Circle Radius 3 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