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
1
Question by Bhashi · Jan 16, 2017 at 01:57 PM · androidmovementplayerdistancefixed

Move player same distance every touch

Hi I want my player to move A to B, B to C by touching right arrow and C to B, B to A by touching left arrow as shown in attached image. Because my player wants to catch balls come from "*" position marked as attachment. I used two difference method for implement player movement. But It doesn't stop at A,B or C position all time. Some time it goes far or less. Event Trigger is used to arrow touch detection.

My target is for android device and I use unity 5.4 and c# to develop my game. It's 2D game.

alt text

The Code I used is put below. I have commented the second method I used.

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Boundary
 {
     public float leftBoundry, rightBoundry;
 }
 public class MovePlayer : MonoBehaviour
 {
 
     public Boundary boundary;
 
     public float speed;
     Rigidbody2D playerRigBody;
     Vector2 movement;
     float input = 0;
 
     void Start()
     {        
         playerRigBody = this.GetComponent<Rigidbody2D>();
     }
 
     void FixedUpdate()
     {
         PlayerMovement(input);
     }
 
     void PlayerMovement(float horisontalInput)
     {
         //movement.x = playerRigBody.velocity.x;
         //playerRigBody.velocity = movement;
         //movement = playerRigBody.velocity;
         //movement.x = horisontalInput * speed * Time.fixedDeltaTime;
         //playerRigBody.velocity = movement;
 
 
         movement = playerRigBody.position;
         movement.x = movement.x + (0.4f * horisontalInput);
         playerRigBody.position = movement;
 
         
         playerRigBody.position = new Vector2
        (
            Mathf.Clamp(playerRigBody.position.x, boundary.leftBoundry, boundary.rightBoundry),
            -3.77f
        );
     }
 
     public void UserInput(float horisontalInput)
     {
         //prevent player move beyond the boundy after met boundry
         //if ((horisontalInput == 1 && playerRigBody.position.x >= boundary.rightBoundry) || (horisontalInput == -1 && playerRigBody.position.x <= boundary.leftBoundry))
         //{
         //    input = 0;
         //}
         //else {
         //    input = horisontalInput;
         //}
 
         input = horisontalInput;
     }
 }





screenshot.png (85.1 kB)
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 doublemax · Jan 16, 2017 at 01:57 PM 0
Share

Please show the relevant code.

avatar image Bhashi doublemax · Jan 16, 2017 at 06:29 PM 0
Share

@doublemax I updated question with the code. please have a look

avatar image doublemax Bhashi · Jan 17, 2017 at 09:54 AM 0
Share

I can't spot any obvious error, are you sure the method "UserInput" gets called with correct values? If you would a Debug.Log into there and check i the input is always 1.0 or -1.0.

2 Replies

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

Answer by Bhashi · Jan 18, 2017 at 10:38 AM

My solution using @iamsidv's pseudocode

 using UnityEngine;
 using System.Collections;
 
 public class MovePlayer : MonoBehaviour
 {
     float[] xPointsArray;
 
     int pointIndex = 0;
 
     public float speed;
     Rigidbody2D playerRigBody;
 
     void Start()
     {
         xPointsArray = new float[3] { -4f, 0, 4f };    
         playerRigBody = this.GetComponent<Rigidbody2D>();
     }
    
     void FixedUpdate()
     {
         PlayerMovement();
     }
 
     void PlayerMovement()
     {
         playerRigBody.transform.position = Vector3.MoveTowards(transform.position, new Vector3(xPointsArray[pointIndex], transform.position.y, transform.position.z), Time.deltaTime * speed);
 
     }
 
     public void RightClick()
     {
         if (pointIndex < (xPointsArray.Length - 1) && pointIndex != 2)
         {
             pointIndex++;
         }
     }
 
     public void LeftClick()
     {
         if (pointIndex <= (xPointsArray.Length - 1) && pointIndex != 0)
         {
             pointIndex--;
         }
     }
 
 }


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
1

Answer by iamsidv · Jan 17, 2017 at 02:15 PM

Hey, If I were you, probably I would make a public float array that would contain all the X-Positions (For Example : -5f,0f and 5f), and I would have a variable called index which will increment till the length of the array while you're pressing the right button and vice versa.

And in the end, i would use a MoveTowards function to move at particular position in x axis, keeping the relevant positions at the same point.

I am writing a pseudocode here :-

 float []  xPoints = new float[3]{-5f,0,5f};
 int index = 0;

 void RightClick(){
 if(index is less than the length of the array minus 1)
   index ++;
 }
 
 void LeftClick(){
 //Vice versa of RightClick function.
 }

and in the update function...

 transform.position = Vector3.MoveTowards(transform.position, new Vector3(xPoints[index], transform.position.y, transform.position.z), Time.deltatime * 10f);

Hope this helps in your case. Cheers !!!

Comment
Add comment · Show 2 · 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 Bhashi · Jan 18, 2017 at 09:57 AM 0
Share

Thank you @iamsidv. I have found the solution using your pseudocode.

avatar image iamsidv Bhashi · Jan 21, 2017 at 07:47 AM 0
Share

Hey, welcome. I'm glad to know that the solution I gave worked out for you. If you could upvote my pseudocode answer, then that would be great. Cheers

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do i move my player left and right using touch pad? 1 Answer

Move the enemy object opposite to player and keep its distance relative to the player? 1 Answer

Raycast distance affected by momentum of character 0 Answers

Shadow distance not working on android 0 Answers

Android export errors 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