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 javaBroker · Sep 30, 2017 at 08:37 AM · movementtouchvelocity

Please help me to move my player a fixed distance

player is moving now only when tap on the screen which is what I wanted. but he is not moving in a fixed amount of distance. for example I want him to move 2 units on the X but he move in a different float value each time such as 1.869 units and if i try to move him back to 0 then he always come back to 0 at different values such as 0.126, 0.420 etc

do you have any idea what can I modify in the code to move him the fixed amount of distance? maybe it's something to do with the sensitivity of the touch or the velocity?

I did Debuging to check how the player move in real time and to my surprise he is moving the amount needed all the way till the end of the distance but only in CODE. because in the EDITOR in the transform values I can see the X position is a Float...

here is the code as is now:

 void Update() {
  
        if (Input.touchCount > 0) {
  
            if (Input.GetTouch(0).phase == TouchPhase.Began) {
          
            touchPosition = Input.GetTouch (0).position;
          
            if (touchPosition.x > screenCenter) {
                target = 2f;
            }
            if (touchPosition.x < screenCenter) {
                target = -2f;
            }
                isTap = true;
                //moveHorizontal = Mathf.MoveTowards (moveHorizontal, target, sensitivity * Time.deltaTime);
            }
        }
  
        if (isTap == true) {
            moveHorizontal = Mathf.MoveTowards (moveHorizontal, target, sensitivity * Time.deltaTime);
            if (moveHorizontal == 2f || moveHorizontal == -2f) {
                isTap = false;
            }
        }
  
        if (isTap == false) {
            moveHorizontal = Mathf.MoveTowards (moveHorizontal, 0, sensitivity * Time.deltaTime);
        }
  
        movement = new Vector3 (moveHorizontal, 0.0f, 0.0f);
    }

second part:

     void FixedUpdate () {
      
            rb.velocity = movement * speedHorizontal;
      
                //restrict movement to boundaries
                if (transform.position.x <= xMin) {
                    transform.position = new Vector3 (xMin, transform.position.y, transform.position.z);
                }
                if (transform.position.x >= xMax) {
                    transform.position = new Vector3 (xMax, transform.position.y, transform.position.z);
                }
      
                //tilt the player when move horizontal
            rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
      
                //keep move forward while playing
              rb.velocity += Vector3.forward * speedForward;
        }
 

thank you

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
0

Answer by santoshabd · Apr 29, 2020 at 10:45 AM

This might be too late to answer this question, but i thought it might help others searching for the same thread. The thing is you have to make sure update runs fixed number of times every frame. If you use just "Update" this might not run fixed number of times every frame. So use "Fixed Update" instead. The other way to do this is define your own number, which is the number of times the update should run per frame using the property Time.fixedDeltaTime.

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

167 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 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 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

Move NPC on triggerEnter 0 Answers

Code help with mobile touch drag 3D Top Down 0 Answers

Can't move player with this code why?,I cannot move my player with this code why? 0 Answers

Up Velocity 1 Answer

2D : How to overwrite rigidbody velocity when "skill" input pressed ??? 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