• 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 KeshaDiamond · Jul 29, 2018 at 12:27 PM · unity 5unity 2dmovevector

Moving Player

Please tell me how to make the object move like in the picture. Circle - where the player pressed the mouse button. Red square - where the player released the mouse. Arrow - where the object should fly.

I need the code to work like this: clicked on the mouse button, chose the direction, let's say it up, let it go and it flew in the opposite direction, that is, to the left. If I released in the upper right corner, then poltelel in the lower left corner

By itself this code works, but the object does not fly down, but somewhere to the left.

     if (Input.GetMouseButton (0)) {
             if (GameObject.Find ("Player(Clone)") == null) {
                 Instantiate (player); // Создаём игрока
             }
 
             firstPos = pl.transform.position; // Получаем первую позицию
             Vector2 camPos = Camera.main.ScreenToWorldPoint (new Vector2 (Input.mousePosition.x, Input.mousePosition.y));
             pl.transform.localPosition = camPos; // Перетаскиваем игрока мышкой
             Debug.Log(twoPos);
         }
 
         if (Input.GetMouseButtonUp (0)) {
             twoPos = pl.transform.position; // Получаем вторую позицию игрока
             twoPos *= -1; // Умножает на -1, чтобы потом игрок полетел в противоположную сторону.
             twoPos = transform.TransformPoint(twoPos);
             pl.AddForce (twoPos * 50);

alt text

121741-psgwr0a7ja0.jpg (5.6 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MT369MT · Jul 29, 2018 at 01:37 PM

Hi, if I understand right you are trying to do something like the angry birds shoot. I tried to modify your script so:

         if (Input.GetMouseButtonDown(0))
         {
             firstPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         }
 
         if (Input.GetMouseButtonUp(0))
         {
             twoPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Vector2 direction = (firstPos - twoPos);
 
             pl.AddForce(direction * 50);
         }
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 KeshaDiamond · Jul 29, 2018 at 01:49 PM 0
Share

Hi. This script works correctly only if you press in the center of the map, but if you press and shoot in the left corner somewhere, it flies to the left in independence where the ball was directed

avatar image KeshaDiamond · Jul 29, 2018 at 01:52 PM 0
Share

I want to do this: clicked the mouse button and the player (ball) appeared and if I want to make it fly down, then move the player up and release the mouse button and it starts to fly down. But if I want to make it fly up, then move the player down and let it go and it should fly upwards

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Screen scaling causing text to be outside button 0 Answers

Second Coroutine isn't working Unity 1 Answer

Instantiated object not showing in scene or hierarchy 2 Answers

I am confused why my enemy gameobject not following the path i created when not using nested if ? 1 Answer

Help with Generating level 0 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