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

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

How do i prevent the player from dragging a ui element off screen? 1 Answer

MoveTowards problem 1 Answer

What am I doing wrong? 1 Answer

Hello everyone, I would like to create a Geometry dash like triangle Transporter in unity. 0 Answers

How do I change Raycast's direction based on movement input? 1 Answer


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