- Home /
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.
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;
}
}
@doublemax I updated question with the code. please have a look
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.
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--;
}
}
}
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 !!!
Thank you @iamsidv. I have found the solution using your pseudocode.
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
Follow this Question
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