- Home /
2d click to move
So I am making a 2D game and every time I clicked on the screen space, my player would swims upward by a little bit. My next goal is to get my player to move left or right. So how do i make my player move right once i click on the right side of the screen space? I tried with ...
target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
it works ok but it's not what i want. I don't want my player to follow the mousePosition, i just want it to move right once the right side of the screen space is clicked. if i clicked left, it goes left.
I also tried ....
if (target.x <= target.z)
{
transform.position = new Vector3( 5, 0, 0);
}
but that kind a transport my character to where i clicked. Sorry, i didn't upload the script. my entire script is too messy right now because I've tried many different method to get this to work but no luck.
Answer by MatiasPi · May 14, 2016 at 09:42 AM
void Update () {
if(Input.GetKey(KeyCode.Mouse0)){
if(Input.mousePosition.x > Screen.width / 2){
Debug.Log("moving right");
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
else{
Debug.Log("moving left");
transform.Translate(-Vector3.right * speed * Time.deltaTime);
}
}
}
hey thanks for the script. the only problem is that
transform.Translate(Vector3.right speed Time.deltaTime);
is moving my player in an instant, not smoothly though. However, I did switch it to
GetComponent ().AddForce (new Vector2 (30, jumpForce));
when I did however, it does not allow for a smooth transition when the player is clicking from left to right and vice versa. The reaction time is slow and delayed. For example, if the player is tapping left the fish will move slowly to the left but when tapping to the right to move right, the fish will continue moving left for 3 seconds then move right. I mean this is 70% how i wanted, but if there's no other way to get around this, i'll stick to this control.
When you use AddForce, you're using Unity's Physics Engine, which may not be necessary. You could make the movement smoother without using physics. One way that's in the top of my head would be that each time you press the screen, you add a small value to a variable called "speed", so the movement speed will increase the more you press the screen. Then you reset "speed" when you're not touching the screen. Sort of like this:
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse0)){
speed += 0.1f; //$$anonymous$$aybe you should cap this value to a certain maximum speed
if(Input.mousePosition.x > Screen.width / 2){
Debug.Log("moving right");
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
else{
Debug.Log("moving left");
transform.Translate(-Vector3.right * speed * Time.deltaTime);
}
}
else{
speed = 0f;
}
If you don't $$anonymous$$d using Physics, then the quick fix for your problem would be to use this line to stop the GO movement when you're not touching the screen:
//rb is a RigidBody variable. Get the reference of the Game Object Rigidbody at the Start() or Awake() method, by doing this:
rb = GetComponent<RigidBody>();
//then, to stop the movement of your game object:
rb.velocity = Vector3.zero;
Omg thank you so so much $$anonymous$$atias! you have no idea how long it took me to get this right but thanks to you I finally solved this.
I ended up using Physics because it works really well when player is in the ocean all the time. I did everything you told me except i changed the
rb2d.velocity = vector3.zero; to rb2d.velocity = vector3.right // if player clicked on the right side.
you're the best man, thanks for taking the time to help a rookie like me ><.
Answer by JedBeryll · May 14, 2016 at 07:49 AM
You are setting the transform position to 5,0,0. If you want to move it you have to translate it or add to the positions. For example this would teleport your character to the right:
transform.position += new Vector3( 5, 0, 0);
lol you're right, it did teleport to the mouse cursor. But how do i move the player to the right though if i click on the right screen. i tried add in time.deltaTime, and it didn't do anything.
time.deltaTime multiplied by the direction you want to move and multiplied by a movement speed
Time.deltaTime * direction * speed
Your answer
Follow this Question
Related Questions
Normalizing a cube made up of 24 meshes into a Sphere 0 Answers
Vector3.distance is always returning 0 4 Answers
How to keep the same position of the object instead of camera 0 Answers
How do I know that an object is not coliding without using OnCollisionExit? 1 Answer
Alternating buttons to keep moving. 2 Answers