- Home /
Do something when there is no mouse click
Hey,
I have a question about OnMouseDown,
Right now I have an if statement on in that says that if the mouse is down then do this.
Else aplication.loadlevel etc
Right now it only loads the other scene if I click anywhere except the object the script is attached to.
I want that to happen, but I want it to also load that scene if there is no mouse click at all.
Thanks
using UnityEngine;
using System.Collections;
public class $$anonymous$$outh : $$anonymous$$onoBehaviour {
GameObject other;
public AudioClip $$anonymous$$outhReaction;
void Start (){
other = GameObject.FindGameObjectWithTag("Commands");
}
void On$$anonymous$$ouseDown (){
if(Commands.Command == 1)
{
other.GetComponent<Commands>().Respawn();
audio.PlayOneShot($$anonymous$$outhReaction);
Score.GameScore += 50;
}
else
Application.LoadLevel ("Lose");
print("You Lose");
}
}
Thanks, could you also provide your "Commands" script please.
Hey Sorry forgot about that! Here it is: using UnityEngine; using System.Collections;
public class Commands : $$anonymous$$onoBehaviour {
public float Speed = 0.5f;
public static int Command = 0;
public static int LastCommand = 0;
public float TimeLeft = 0;
public Texture2D mouth;
public Texture2D eye;
public Texture2D nose;
public Texture2D ear;
private Texture2D myimage;
public AudioClip $$anonymous$$outhCommand;
public AudioClip EyeCommand;
public AudioClip EarCommand;
public AudioClip NoseCommand;
void OnGUI (){
GUI.DrawTexture(new Rect(Screen.width / 3, Screen.height / 11, 200,100), myimage);
}
void Start (){
Command = Random.Range(0,5);
}
void Update (){
TimeLeft += Time.deltaTime;
if(TimeLeft >2)
{
Respawn();
}
transform.Translate(Vector3.up * Time.deltaTime * Speed); // Translate it up
if(transform.position.y >= 1.65f) // Respawn it
{
Respawn();
}
if(LastCommand != Command)
{
LastCommand = Command;
switch(Command)
{
case 1:
myimage = mouth;
audio.PlayOneShot($$anonymous$$outhCommand);
break;
case 2:
myimage = eye;
audio.PlayOneShot(EyeCommand);
break;
case 3:
myimage = nose;
audio.PlayOneShot(NoseCommand);
break;
case 4:
myimage = ear;
audio.PlayOneShot(EarCommand);
break;
}
}
}
public void Respawn (){
TimeLeft = 0;
transform.position = new Vector3(Random.Range(0.2f,0.9f), 0.4f, transform.position.z);
Command = Random.Range(1,5);
}
}
Also, any help you give me, I would rather you don't just give me the code I need in ym current scripts. I'd rather you just give me the snippets I need and then I can learn and place them where needed. I'd rather learn a little then just be given the answer fully :)
Do you want the scene to load after a certain amount of inactivity or do you want the mouse click to occur and load the level regardless? Im kinda lost as to what you actually want
Answer by SubatomicHero · May 13, 2013 at 09:05 AM
Ok so what you need is an update function within your Mouth class like this:
// declared at the top with your other class variables
float timeOut = 5.0f; // 5 seconds
private float timer = 0.0f;
// Then the update function
Void Update()
{
timer += Time.deltaTime;
if (Input.GetAxis("Mouse X") || Input.GetAxis("Mouse Y"))
{
// the player moved the mouse, reset the timer
timer = 0.0f;
}
if (timer > timeOut)
{
// inactivity for a period has occured, do what you want
}
}
This obviously only checks for mouse movement, but you can easily change the checks to check for a mouse key press
hmmm the code brings up this error
Operator ||' cannot be applied to operands of type
float' and `float'
Double check that mouse x and mouse y are valid axis in your project setup.
But its a typical boolean statement which is used in alot of Update functions.
You could even test it with:
if (Input.Get$$anonymous$$eyUp($$anonymous$$eycode.Space))
{
// if the spacebar has been pressed
timer = 0.0f;
}
Hey, I know this is an old post but I'm pretty sure it should help me...I just can't get it to work.
Like some other people I get the error - ||' cannot be applied to operands of type float' and `float.
And like you suggested I checked my $$anonymous$$ouseLook and I'm pretty sure that my mouse x and y are valid axes. I even tested your second code and I got that to work! But no luck with the code I actually need.
What I want it to do is, when left idle, the game loads the first scene.
Here is my Idle script (thought I should attach incase I've made a stupid mistake):
public string level;
float timeOut = 20.0f; // 20 seconds
private float timer = 0.0f;
// Then the update function
void Update()
{
timer += Time.deltaTime;
if (Input.GetAxis("$$anonymous$$ouse X") || Input.GetAxis("$$anonymous$$ouse Y"))
{
// the player moved the mouse, reset the timer
timer = 0.0f;
}
if (timer > timeOut)
{
Application.LoadLevel(level);
}
}
}
And my $$anonymous$$ouseLook script:
public enum RotationAxes
{
$$anonymous$$ouseXAndY = 0,
$$anonymous$$ouseX = 1,
$$anonymous$$ouseY = 2
}
public RotationAxes axes = RotationAxes.$$anonymous$$ouseXAndY;
public float sensitivityHor = 9.0f;
public float sensitivityVert = 9.0f;
public float $$anonymous$$imumVert = -45.0f;
public float maximumVert = 45.0f;
private float _rotationX = 0;
void start()
{
Rigidbody body = GetComponent<Rigidbody>();
if (body != null)
body.freezeRotation = true;
}
// Update is called once per frame
void Update()
{
if (axes == RotationAxes.$$anonymous$$ouseX)
{
//horizontal rotation here
transform.Rotate(0, Input.GetAxis("$$anonymous$$ouse X") * sensitivityHor, 0);
}
else if (axes == RotationAxes.$$anonymous$$ouseY)
{
//vertical rotation here
_rotationX -= Input.GetAxis("$$anonymous$$ouse Y") * sensitivityVert;
_rotationX = $$anonymous$$athf.Clamp(_rotationX, $$anonymous$$imumVert, maximumVert);
float rotationY = transform.localEulerAngles.y;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
else
{
//both horizontal and certical rotation here
_rotationX -= Input.GetAxis("$$anonymous$$ouse Y") * sensitivityVert;
_rotationX = $$anonymous$$athf.Clamp(_rotationX, $$anonymous$$imumVert, maximumVert);
float delta = Input.GetAxis("$$anonymous$$ouse X") * sensitivityHor;
float rotationY = transform.localEulerAngles.y + delta;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
}
}
Can you see what I'm doing wrong? Sorry to bring up such an old post but I'm pretty new to unity and, by now, totally confused by why this isn't working. Thanks!
Shouldn't it be something more like this..?
if (Input.GetAxis("$$anonymous$$ouse X") != 0 || Input.GetAxis("$$anonymous$$ouse Y") != 0)
(or checking if the magnitudes of the return values are less than some threshold, basically the idea is you want to generate bools from the axis values)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to keep my cursor inside window boundaries 0 Answers
OnMouseDown for Right Mouse 2 Answers
How to rotate an object around another facing to mouse? 1 Answer
Distribute terrain in zones 3 Answers