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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by SBull · Sep 29, 2013 at 12:05 AM · javascriptcharactercontrollermouseclickmouse-draginput.getmousebuttondown

Prevent Input.GetMouseButtonDown from working anywhere.

I have a game where the player clicks on the character and moves them around with the mouse (basically like the launching functionality in Angry Birds). My problem is that the character moves to the mouse's position if the mouse is clicked anywhere onscreen. I would like it so that the character can be "grabbed" only if the player clicks on the character. The character has a characterController attached. I'm not sure if that makes a difference. Here is my current script:

 function Update()
 {
     if(squirrelActive == false && isLoaded)
     {
         //get mouse point in world space
         var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         pos.z = 0; //makes sure Z = 0
         
         //drag squirrel while button pressed
         if(drag)
         {
             transform.position = pos;
         }
 
         if(Input.GetMouseButtonDown(0))
         {        
             drag = true;
             startPos = pos; //save initial position
         }
         
         if(Input.GetMouseButtonUp(0))
         {
             drag = false;
             var dir = startPos - pos; //calculate direction and intensity
             
             moveDirection = dir * launchForce; //launches with force input
             
             squirrelActive = true;//allows squirrel to begin moving freely
          }
             
      }
 }

I'm pretty new to scripting so any help is greatly appreciated! Thanks!

Comment
Add comment · Show 3
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 Miziziziz · Oct 02, 2013 at 05:09 PM 0
Share

what about putting a collider on the character with the 'player' tag. Then when you click, perform a raycast that checks if what you clicked has the "player" tag.

avatar image SBull · Oct 02, 2013 at 06:22 PM 0
Share

That works! I have to put the WHOLE launch section in the raycast check but it works. Thanks!

Your comment isn't an answer though so I can't check it correct.

avatar image Miziziziz · Oct 02, 2013 at 07:26 PM 0
Share

glad I could help :) I'll reenter it as an answer

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Miziziziz · Oct 02, 2013 at 07:27 PM

what about putting a collider on the character with the 'player' tag. Then when you click, perform a raycast that checks if what you clicked has the "player" tag.

Comment
Add comment · 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
1

Answer by TrickyHandz · Oct 02, 2013 at 03:29 AM

You can simplify this greatly by using the OnMouseDrag() event that is built into Unity. Here is the reference page: OnMouseDrag

 function OnMouseDrag()
 {
     if(squirrelActive == false && isLoaded)
     {
        //get mouse point in world space
        var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        pos.z = 0; //makes sure Z = 0
  
        transform.position = pos;
        startPos = pos; //save initial position
 
        var dir = startPos - pos; //calculate direction and intensity
        moveDirection = dir * launchForce; //launches with force input
        squirrelActive = true;//allows squirrel to begin moving freely
  
      }
 }

As long as there is a collider on the object, you can drag it across the screen. Let me know if this works for you.

Comment
Add comment · Show 3 · 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 SBull · Oct 02, 2013 at 04:56 PM 0
Share

Thanks! I was looking for something like this function when I was making these controls. It solves my initial problem of being able to click anywhere onscreen. Now I have to click on the character to affect it. However, it doesn't work using your exact code. I tried adding this into the On$$anonymous$$ouseDrag function:

 if(Input.Get$$anonymous$$ouseButtonUp(0))
 {
     var dir = startPos - pos; //calculate direction and intensity
             
             moveDirection = dir * launchForce; //launches with force input
 
 squirrelActive = true;//allows squirrel to begin moving freely
 
 }

but it doesn't seem to recognize when I let go of the mouse button. Once I let go of the mouse my character just freezes in midair where I left him which means squirrelActive is not being changed so it's obviously not seeing this section.

avatar image TrickyHandz · Oct 02, 2013 at 05:09 PM 0
Share

@Sbull, On$$anonymous$$ouseDrag() will only pickup the dragging action on the object and won't pick up on Get$$anonymous$$ouseButtonUp(). You could put a bool at the beginning and end and then check your Get$$anonymous$$ouseButtonUp() in Update() with a check like this:

 var IsDragging = false;
 
 function Update()
 {
     if(Input.Get$$anonymous$$ouseButtonUp(0) && !IsDragging)
     {
         // Code to execute when dragging stops
     }
 
 }
 
 function On$$anonymous$$ouseDrag()
 {
     IsDragging = true;
 
     // Code to execute while dragging
 
     IsDragging = false;
 
 }

I will edit the answer with a better example when I get back to my development machine.

avatar image SBull · Oct 02, 2013 at 05:51 PM 0
Share

@TrickyHandz This doesn't exactly work either. The force doesn't seem to be applied to the character when the mouse is released. The character just drops to the ground.

Also if IsDragging is set to false from the start and the player clicks outside of the character, the character is still set in motion when the player releases the mouse.

avatar image
0

Answer by tw1st3d · Oct 01, 2013 at 05:37 PM

This is what I do, I use interfaces and enums to determine --what-- I'm clicking. This is only a small example. The full thing is complex and complicated for a new programmer.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public interface Player
 {
     INTERFACE clickedObj = GlobalVals.theInterface;
     
     public INTERFACE getClicked(){ return clickedObj; }
 }
 
 public class ClickHandler : MonoBehavior
 {
     public void OnMouseDown()
     {
         GlobalVals.clickingTerrain = true;
     }
     
     public void OnGUI()
     {
         Player player = new Player();
         if(player.getClicked() == INTERFACE.TERRAIN)
         {
             GUI.Label(new Rect(20,20,100,20,), "Terrain");
         }
     }
 }
 
 public static class GlobalVals
 {
     public static bool clickingTerrain = false;
     public INTERFACE theInterface;
 }
 
 enum INTERFACE
 {
     TERRAIN,
     HUD,
     GUI,
     MENU
 }
Comment
Add comment · Show 3 · 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 SBull · Oct 01, 2013 at 06:10 PM 0
Share

Sorry, I'm not sure how I would use this. First, I am a new programmer so you're right about it looking complicated. I don't know what you mean by interfaces and enums. Aren't enums just a way of relabeling variables? Also I am using java so none of your code looks familiar to me.

avatar image tw1st3d · Oct 01, 2013 at 07:13 PM 0
Share

An enum is a list of things that can be defined for precise checks, or a list of possible checks. Other than that, I don't work with Unityscript, so I won't be able to help you out there.

avatar image SBull · Oct 02, 2013 at 03:01 AM 0
Share

Okay. Thanks for your advice anyway.

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

18 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

Related Questions

Why doesn't this animation script work? 3 Answers

How can I make a character controller that changes positions on one click. 1 Answer

help with character respawn after death (javascript) 1 Answer

Prevent character from autojumping when moving down a slope. 0 Answers

Setting Scroll View Width GUILayout 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