- Home /
When user touch the screen the object move left right smoothly
Hello everyone. I am developing a 2D game in unity 5. In my game there is one quad (this is my player) and PlayerMove script attached to it.
What I want is when the player touch the right side of the screen, my object start moving to the right direction smoothly and when the user touch the left side of the screen the object start move to the left direction of the screen smoothly.
Need any kind of help
Here is my PlayerMove script but it doesn't work as I want.
using UnityEngine; using System.Collections;
public class MoveScript : MonoBehaviour {
public float PlayerMoveSpeed;
void Start () {
}
void FixedUpdate () {
for (int i =0; i<Input.touchCount; i++) {
Touch touch = Input.GetTouch(i);
if (touch.position.x < Screen.width/2)
{
transform.Translate(Vector3.left * Time.deltaTime * PlayerMoveSpeed);
}
if (touch.position.x > Screen.width/2)
{
transform.Translate(Vector3.right * Time.deltaTime * PlayerMoveSpeed);
}
}
}
}
Answer by junaidijaz · Sep 19, 2017 at 06:34 AM
this works for me
public class playermovement : MonoBehaviour {
public float speed = 7000f;
public float left_right_movment = 90f;
void FixedUpdate ()
{
`if (Input.touchCount > 0)
{
foreach (Touch touch in Input.touches)
{
if (touch.position.x < Screen.width / 2)
{
rd.AddForce(-left_right_movment *
Time.deltaTime, 0, 0,
ForceMode.VelocityChange);
}
else if (touch.position.x > Screen.width /
{
rd.AddForce(left_right_movment *
Time.deltaTime, 0, 0,
ForceMode.VelocityChange);
}
}
}
} }
Your answer
![](https://koobas.hobune.stream/wayback/20220613200831im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers
Vector3 Returning Infinity Error 2 Answers
how do i slowly translate a object to a other objects position 2 Answers
Gradually decrease camera pan speed when touch has ended? (Touch input) 0 Answers
Match position of two object without making one a child of the other 4 Answers