- Home /
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!
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.
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.
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.
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.
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.
@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.
@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.
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
}
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.
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.
Your answer

Follow this Question
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