Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 falconstrike209 · Oct 06, 2020 at 10:09 PM · 2drigidbodyplayer movement

Trying to make a 2d object launch towards the mouse when I left click. can anyone help?

I'm very new to unity and c# and am trying to make the player launch towards the mouse in 2d on a cooldown when I left click. I've tried using raycasts and adding force to the rigidbody, but nothing seems to be working. This seems like it would be a pretty simple code for an experienced person, so if someone could explain it to me that would be much appreciated.

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
2
Best Answer

Answer by IvovdMarel · Oct 06, 2020 at 10:29 PM

Hey Falconstrike!

On your post: I recommend in the future to provide a bit more context to your problem. You can share your own code snippets, and explain in more detail what part is not working (e.g. error message, moving in the wrong direction, not moving at all?).

As to your question, you can see this code snippet that has the feature that you are describing. Hopefully you can make sense of it through the comments.

 public class ShootToMouse : MonoBehaviour
 {
     public float forceAmount = 100;
 
     // Update is called once per frame
     void Update()
     {
         //If left mouse button is pressed
         if (Input.GetMouseButtonDown(0))
         {
             //Get the position of this object in screen-coordinates
             Vector3 posInScreen = Camera.main.WorldToScreenPoint(transform.position);
 
             //You can calculate the direction from point A to point B using Vector3 dirAtoB = B - A;
             Vector3 dirToMouse = Input.mousePosition - posInScreen;
 
             //We normalize the direction (= make length of 1). This is to avoid the object moving with greater force when I click further away
             dirToMouse.Normalize();
 
             //Adding the force to the 2D Rigidbody, multiplied by forceAmount, which can be set in the Inspector
             GetComponent<Rigidbody2D>().AddForce(dirToMouse * forceAmount);
         }
     }
 }
Comment
Add comment · Show 1 · 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 falconstrike209 · Oct 06, 2020 at 10:32 PM 0
Share

thank you!

avatar image
0

Answer by enestelli · Oct 06, 2020 at 11:02 PM

Hello @falconstrike209, first of all you need to find the position of the mouse when it's clicked. Input.mousePosition is the way which we use. But this gives us the pixel coordinates of where the mouse was clicked.

We must use Camera.main.ScreenToWorldPoint() function to translate this into world coordinates. Since we want a shot to be in the direction we click, we need to find the direction vector of this shot.

Vector3 lookDirection = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

This vector shows us in which direction to shoot. Based on this vector, we need to calculate the shooting angle.

float lookAngle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg; Mathf.Atan2 is a function that calculates the vector's angle and returns the angle in radians whose tan is y/x. And Mathf.Rad2Deg is a radians-to-degrees conversion constant. It is equal to 360 / (PI * 2). Thus, we find the angle to be shot according to the clicked position. We finally assign this angle on our own rotation to rotate ourselves in the direction we shoot.

transform.rotation = Quaternion.Euler(0f, 0f, lookAngle - 90);

Then we have to instantiate what we will fire.

GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);

And we need to access the rigidbody component to give it a speed in the specified direction.

bullet.GetComponent().velocity = transform.up * bulletSpeed;

Finally you can use something like this:

 [SerializeField] GameObject bulletPrefab;
 [SerializeField] float bulletSpeed;
 
 private Vector3 lookDirection;
 private float lookAngle;
 
 void Update()
 {
     Rotate();
     if (Input.GetMouseButtonDown(0))
     {
         Fire();
     }
 }
 
 private void Rotate()
 {
     lookDirection = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
     lookAngle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.Euler(0f, 0f, lookAngle - 90);
 }
 
 private void Fire()
 {
     bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
     bullet.GetComponent<Rigidbody>().velocity = position.up * bulletSpeed;
 }
 











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 falconstrike209 · Oct 07, 2020 at 08:08 PM 0
Share

thank you, however, I wasn't trying to fire a bullet. I was actually launching the player towards the mouse. however, I still appreciate the explanation of these functions.

avatar image enestelli falconstrike209 · Oct 07, 2020 at 08:12 PM 1
Share

Wow sadly, I got this question very wrong. I'm happy if I could still teach something.

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

339 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

wasd movement rigidbody no bouncing 0 Answers

Instantiated prefab changes gravitational speed when dragged? 0 Answers

2D Top-Down Movement: Transform.up and right don't change 2 Answers

Player keeps falling halfway through terrain floor 3 Answers

2D rigidbody movement & walls 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