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 unity_mCrWoAIabgW9yw · Mar 21, 2020 at 10:19 AM · 2dinput2d gamemobilemouseposition

Problem with Vector3 Distance and Mouse Input,

Hi, I have a problem with my Mouse Position. So the idea is , that I compare the position of my Mouse with the position of my gameobject. When the distnace of these two is smaller than my maximum distance variable, I destroy my gameobject. But when I play my game nothing happens and the Input isn't registered.

Here is my code:

     public class Tropfen : MonoBehaviour
     {
        public float speed = 10.0f;
        Rigidbody2D rb;
        AudioSource source;
        [SerializeField] AudioClip[] soundClips;
        [SerializeField] GameObject particleEffect;
        float isMuted;
        Gameplay gameplay;
        Camera mainCam;
        [SerializeField] float maxDistance = 0.25f;
 
     // Start is called before the first frame update
     void Start()
     {
         mainCam = Camera.main;
         rb = GetComponent<Rigidbody2D>();
         rb.velocity = new Vector2(0, -speed * Time.deltaTime);
         gameplay = FindObjectOfType<Gameplay>();
         source = GetComponent<AudioSource>();
         source.clip = soundClips[Random.Range(0, soundClips.Length)];
         isMuted = PlayerPrefs.GetFloat("Muted");
         if (isMuted == 0)
         {
             source.volume = 0;
         }
         else if (isMuted == 1)
         {
             source.volume = 1;
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetMouseButton(0))
         {
             GetInput();
         }
     }
 
     private void GetInput()
     {
         Vector3 mousePos = Input.mousePosition;
         Vector3 worldPos = mainCam.ScreenToWorldPoint(mousePos);
         var plane = new Plane(Camera.main.transform.forward, transform.position);
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (plane.Raycast(ray, out var hitDistance))
         {
             var worldPoint = ray.GetPoint(hitDistance);
             var TropfenTouchDistance = Vector3.Distance(worldPos, transform.position);
 
             if (TropfenTouchDistance <= maxDistance)
             {
                 TropfenDestruction();
             }
         }
 
     }
 
     private void TropfenDestruction()
     {
         Destroy(gameObject);
         if (isMuted != 0)
         {
             AudioSource.PlayClipAtPoint(source.clip, transform.position);
         }
         GameObject effect = Instantiate(particleEffect, transform.position, Quaternion.identity) as GameObject;
         Destroy(effect, 2f);
         gameplay.IncreaseScore(1);
     }
 }
   

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 CBV · Mar 21, 2020 at 03:57 PM

So at first theres a problem right at your Destruction Methode: You're destroying the gameobject, which has the script attached, before you check all that other stuff, so it will never happen, because the script also doesnt exist anymore.

Second: I really dont get it, why you're creating a plane, to find out, if you're mouse is in distance or not. So if you're read the documentation for a plane: https://docs.unity3d.com/ScriptReference/Plane.Raycast.html you will see, that your plane always will return false, because the ray is wrong.

If you want to find out, if your mouse is pointing at the object or not, simply use Physics.Raycast:

 Vector3 mousePos = Input.mousePosition;
 Vector3 worldPos = mainCam.ScreenToWorldPoint(mousePos);
 if (Physics.Raycast(worldPos,mainCam.transform.forward,out var hitDistance))
          {
              var TropfenTouchDistance = Vector3.Distance(worldPos, transform.position);
  
              if (TropfenTouchDistance <= maxDistance)
              {
                  TropfenDestruction();
              }
          }
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 unity_mCrWoAIabgW9yw · Mar 23, 2020 at 10:57 AM 0
Share

I used your code , but it didn't work. So I used the DrawGizmo $$anonymous$$ethod to see , where my mouse is pointing to and found out that the point , to which my mouse is pointing to ,is outside of my camera and canvas. Could it have something to do with my camera or canvas settings?

avatar image CBV unity_mCrWoAIabgW9yw · Mar 23, 2020 at 11:48 AM 0
Share

Try something like this: RaycastHit hit; Ray ray = camera.ScreenPointToRay(Input.mousePosition);

         if (Physics.Raycast(ray, out hit)) {
             if(hit.gameobject.GetComponent<Tropfen>()
 {
 //Do stuff
 }
         }

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

315 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

Related Questions

Input.mousePosition equivalent to first finger touch? 3 Answers

Use Standard Assets Mobile Input Script 0 Answers

How can I stop the player movement from being too fast while moving diagonally in my 2D game? 2 Answers

is possible press 2 buttons at the same time Mobile touch? 0 Answers

How to use crossplatform input touchpad to control space-invader style game 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