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 /
avatar image
0
Question by 4shm3x · May 13, 2013 at 01:06 AM · c#mouseonmousedown

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

Comment
Add comment · Show 6
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 SubatomicHero · May 13, 2013 at 07:27 AM 0
Share

Could you provide your script for that please?

avatar image 4shm3x · May 13, 2013 at 08:20 AM 0
Share
 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");
 }
 }
avatar image SubatomicHero · May 13, 2013 at 08:24 AM 0
Share

Thanks, could you also provide your "Commands" script please.

avatar image 4shm3x · May 13, 2013 at 08:28 AM 0
Share

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 :)

avatar image SubatomicHero · May 13, 2013 at 08:40 AM 0
Share

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

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 6 · 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 4shm3x · May 13, 2013 at 09:48 PM 1
Share

hmmm the code brings up this error

Operator ||' cannot be applied to operands of type float' and `float'

avatar image KnightRiderGuy 4shm3x · Dec 06, 2015 at 06:12 PM 0
Share

@4shm3x I get the same error too?

avatar image SubatomicHero · May 14, 2013 at 06:37 AM 0
Share

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;
 }
avatar image Crucible1 SubatomicHero · Jan 20, 2016 at 02:08 PM 0
Share

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!

avatar image Bonfire-Boy Crucible1 · Jan 20, 2016 at 02:42 PM 1
Share

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)

Show more comments

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

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


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