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
0
Question by Dqimass · May 14, 2016 at 05:12 AM · unity 52d gamevector3screentoworldpointclicktomove

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.

Comment
Add comment
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

2 Replies

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

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);
             }
         }
     
     }
Comment
Add comment · Show 3 · 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 Dqimass · May 16, 2016 at 12:27 AM 0
Share

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.

avatar image MatiasPi Dqimass · May 16, 2016 at 12:40 AM 0
Share

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;

avatar image Dqimass MatiasPi · May 16, 2016 at 08:06 AM 0
Share

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 ><.

avatar image
0

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);
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 Dqimass · May 14, 2016 at 08:40 AM 0
Share

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.

avatar image JedBeryll Dqimass · May 14, 2016 at 09:07 AM 0
Share

time.deltaTime multiplied by the direction you want to move and multiplied by a movement speed

 Time.deltaTime * direction * speed

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

76 People are following this question.

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

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


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