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 /
  • Help Room /
avatar image
0
Question by Stockler · Dec 04, 2015 at 03:55 PM · 2d-physicsphysics material

Move an Object by clicking on it

Hello guys, i've been searching for somedays now and didnt find my answer. I'm trying to make a 2D game,where when i click in a ball make it moves, but i'm getting problems to know the position of the mouse and the object. This is what i have:

 using UnityEngine;
 using System.Collections;
 
 public class moveBubble : MonoBehaviour {
 
     Vector2 mousePos;
     Vector2 ballPos;
     Vector2 forceDir;
 
     float factor = 0.003f;
     Rigidbody2D objFis;
 
     void Start()
     {
         objFis = objFis = this.gameObject.GetComponent<Rigidbody2D>();
     }
 
     IEnumerator OnMouseUp ()
     {
         mousePos = Input.mousePosition;
         ballPos = this.gameObject.transform.position;
 
         forceDir = mousePos - ballPos;
     
         objFis.AddForce(forceDir * factor);
         yield return StartCoroutine(StopBall(4.0F));
     }
 
     IEnumerator StopBall(float waitTime)  {
         yield return new WaitForSeconds (4); 
         objFis.velocity = Vector3.zero;
     }
     
 }
 

The direction is not working at all, i dont know if i should use a raycast to know where the mouse is on the object, and even the force its too much. I already tried to make a lot if conditions, and add an expecific force to the object, but its just so many ifs that feels wrong doing it. The ball has a physics2d, and 0 gravity. Its a ballon in the space basically The logic is simple, if i click in the button of the ball, it moves up, etc... i want all the directions

Can anyone help me? Thanks!

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

Answer by JedBeryll · Dec 04, 2015 at 11:12 PM

Yes raycasting is the best way to do it. Input.mousePosition will give you wrong coordinates because its a screenpoint coordinate. (on the screen the y works backwards)`enter code here` I have something similar:

 public Caster caster; // this is a reference to my raycaster script
 
 void Update() {
     if (caster.hit.collider != null) { //make sure you actually hit something otherwise you get errors
         mousePos = caster.hit.point;
     }
 }

The Caster has a RaycastHit hit; variable which i set every frame. So if you want to decide where the ball should go by clicking on it and deciding if its up, or down or whatever compared to the ball's center:

 ballPos = this.gameObject.transform.position;
     
 forceDir = mousePos - ballPos;
 forceDir.Normalize(); // do this to make sure your vector has a magnitude of 1 - this causes the ball to move with the intended speed
 
 objFis.AddForce(forceDir * factor);

Now you should scale up your factor because it's going to move unnoticably slow. Hope this helps!

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 Stockler · Dec 07, 2015 at 01:00 PM 1
Share

Thanks a lot! That worked :D i only had to change the forceDir = ballPos - mousePos to get what i really wanted :) thank you

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unrealistic 2D Bouncing 2 Answers

Problem getting dropped bombs to behave properly 0 Answers

2D ledge grabbing -1 Answers

Max velocity and constant acceleration 0 Answers

AddForce making jumping way too quick (2D) 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