Object heading towards mouse click/ touch position, script improvement.
Hi all,
Just so it is clear, I am quite new to the programming.
I am learning by creating my first game for android , it is a simple Breakthrough/ Arkanoid clone. I got my script for the paddle working ok. But what I am looking for is the improvement in the behavior. Paddle moves left and right as it should. What I can't get it to do is that second touch overrides first touch and paddle will change direction of the movement. For example: If I touch the screen on the left side of the paddle it will move to the left until the edge of the screen, but if I want it to start moving to the right I need to release the finger before I touch the screen on the right side of the paddle. If I press it too early on the right, paddle will continue going to the left until I release the finger and press it again on the right side of the paddle.
P.S. I have used Input.GetMouseButtonDown as I am familiar with this method and I have read that will work on the phone(so far it does, tested with Unity Remote 5 and by uploading my app on the phone directly). Movement is simple left or right so at the moment I am not looking for new method unless you think Input.GetMouseButtonDown can generate some issues.
public class PaddleMovement : MonoBehaviour
{
public float speed = 1.5f;
private Vector3 mouseClickPosition;
void Start()
{
mouseClickPosition = transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
mouseClickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mouseClickPosition.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, mouseClickPosition, speed * Time.deltaTime);
}
}
I want to understand it, not just copy the code if it is not to crazy I mean ;)
Thanks for help.
HoOrza
Answer by Andrea_Marchetti · May 18, 2017 at 06:49 PM
The Input.mousePosition is not designed for mobiles. What it probably does on mobile is that it takes the average point between all the points you are touching on the screen. Furthermore, but I am not sure, on iOS it doesn't work as well as on Android.
For mobile development use instead Input.GetTouch. It should be also faster too. If you want your character to follow the input of the last touch then you can check when Input.touchCount changes and allow your code to use the last item inserted into the array as the reference or you can check the TouchPhase of all touches and assign to a new variable the index of the one that has the phase equal to TouchPhase.Began.
Thanks @Andrea_$$anonymous$$archetti , I will look into it shortly when I start polishing my game. Once I got the right code and solution I will post it. I am sure there is a way to override first touch with second touch.
Your answer
Follow this Question
Related Questions
how to make your object move from point A to point B? 1 Answer
My bullet keeps following its spawn point after being instantiate. Pls help 1 Answer
How to spawn an object on button click and make it follow the mouse until left click? 0 Answers
How do I get a character to walk on walls and ceilings? 1 Answer