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 deathbane · May 03, 2014 at 06:58 PM · c#2dphysicsballlaunch

I am trying to launch a 2d object toward the mouse.

I am trying to make a ball launch towards the mouse in 2d, but it is going in directions that don't make much sense.

 using UnityEngine;
 using System.Collections;
 
 public class Jolt : MonoBehaviour {
     public Camera veiw;
     public float joltSpeed;
     private Vector3 mousecords = new Vector3(0,0,0);
 
 
     void Update (){
 
         if(Input.GetMouseButtonDown(0)){
             mousecords = veiw.ScreenToWorldPoint(Input.mousePosition);
             mousecords = new Vector3(mousecords.x,mousecords.y,0);
             transform.rotation = Quaternion.Euler(0,0,Vector3.Angle(transform.position,mousecords));
             rigidbody2D.AddForce(transform.right * joltSpeed);
 
         }
     }
 }
 
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
1
Best Answer

Answer by robertbu · May 03, 2014 at 07:18 PM

Here is a bit of rewrite to your code. 'mousecoords' is no longer needed. It assumes the 'right' side of the object this script is attached to is considered the 'forward' side (which is a good guess since you are using 'transform.right' in your AddForce()'. If the 'up' side is considered forward, add 90 to the angle before doing the AngleAxis() rotation.

 void Update (){
 
    if (Input.GetMouseButtonDown(0)) {
      var pos = veiw.ScreenToWorldPoint(Input.mousePosition);
      var dir = pos - transform.position;
      var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
      transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
      rigidbody2D.AddForce(transform.right * joltSpeed);
    }
 }
Comment
Add comment · Show 4 · 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 deathbane · May 03, 2014 at 07:57 PM 0
Share

Could you be more specific where to add 90 degrees?

avatar image deathbane · May 03, 2014 at 07:59 PM 0
Share

Also dir is never used, and I am not sure if your are using javascript or c#, c# is preferred.

avatar image robertbu · May 03, 2014 at 08:33 PM 0
Share

Sorry. Fixed the 'dir' problem. If you are using Vector3.right for your AddForce, then you likely should not be adding 90 degrees. But if you do then:

 transform.rotation = Quaternion.AngleAxis(angle+90, Vector3.forward);

But if you make that change, you likely want to use transform.up in your AddForce() call.

avatar image deathbane · May 03, 2014 at 08:37 PM 0
Share

Thanks! works perfectly now.

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

20 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

Related Questions

Help with 2D physics script 1 Answer

Can anyone tell me what I'm doing wrong here? 1 Answer

Rotate towards velocity (2D) 2 Answers

2D swinging physics. 0 Answers

How to move a GameObject around with physics 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