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 Theyrr · Oct 23, 2013 at 02:36 PM · c#guiraycastmousetower defense

OnMouseUp() not working like meant

I am building a tower defense, and I want to upgrade my turrets. I've been trying to use OnMouseUp and raycasting, but both have failed. When using OnMouseUp, it doesn't matter where I click, it will run the function anyways. And when using raycasting, the same thing happens, or nothing will happen at all.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Cannon : MonoBehaviour {
     public GameObject projectile;
     public float reloadTime = 1f;
     public float rotateSpeed = 5f;
     public float cooldown = .25f;
     public GameObject muzzleEffect;
     public float errorAmount = .001f;
     public Transform target;
     public Transform[] muzzlePositions;
     public Transform turretBall;
     public GameObject aimHere;
     public List<GameObject> targetList = new List<GameObject>();
     public float damage;
     public RaycastHit hit = new RaycastHit();
     public Ray ray;
     protected bool showUpgrades;
     
     private float nextFireTime;
     private float nextMoveTime;
     private Quaternion desiredRotation;
     private float aimError;
 
     void Start() {
         damage = 12.5f;
         showUpgrades = false;
     }
 
     void Update (){
         if(target) {
             if(Time.time >= nextMoveTime) {
                 CalculateAimPosition(target.position);
                 turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * rotateSpeed);
             }
             
             if(Time.time >= nextFireTime) {
                 FireProjectile();
             }
 
         }
 
         if(target == null && targetList.Count >= 1) {
             target = targetList[0].transform;
             aimHere = target.gameObject;
         }
     }
     
     void  OnTriggerEnter (Collider other){
         if(other.gameObject.tag == "AimPoint") {
             nextFireTime = Time.time + (reloadTime * .5f);
 //            target = other.gameObject.transform;
             targetList.Add(other.gameObject);
         }
     }
     
     void  OnTriggerExit (Collider other){
         if(other.gameObject.tag == "AimPoint") {
             targetList.Remove(other.gameObject);
         }
     }
     
     void  CalculateAimPosition (Vector3 targetPos){
         //Vector3 aimPoint = new Vector3(targetPos.x + aimError, targetPos.y + aimError, targetPos.z + aimError);
         Vector3 aimPoint = new Vector3(aimHere.transform.position.x, aimHere.transform.position.y, aimHere.transform.position.z);
         if (aimPoint != turretBall.position) {
             desiredRotation = Quaternion.LookRotation(turretBall.position - aimPoint);
         }
     }
     
     void  CalculateAimError (){
         aimError = Random.Range(-errorAmount, errorAmount);
     }
     
     void  FireProjectile (){
         //audio.Play();
         nextFireTime = Time.time + reloadTime;
         nextMoveTime = Time.time + cooldown;
         CalculateAimError();
         
         foreach(Transform theMuzzlePos in muzzlePositions) {
             Instantiate(projectile, theMuzzlePos.position, theMuzzlePos.rotation);
             //Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
         }
     }
 
     public void TargetIsDead() {
         Debug.Log("TargetIsDead");
         targetList.RemoveAt(0);
         FetchNextTarget();
     }
 
     void FetchNextTarget() {
         Debug.Log (targetList.Count);
     }
 
     void OnMouseUp() {
         if(showUpgrades) {
             showUpgrades = false;
         } else {
             showUpgrades = true;
         }
     }
 
     void OnGUI() {
         if(showUpgrades) {
             if(GUILayout.Button("Upgrade")) {
                 damage = damage + 10;
                 Debug.Log("Upgraded");
             }
         }
     }
 }
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 Noztradamuz · Oct 23, 2013 at 06:14 PM

One advice on the OnMouseUp() function you have, there's an easiest way to do that, it's called a boolean flag, something like this:

     void OnMouseUp() 
     {
       ShowUpgrades = !ShowUpgrades;
     }

what this does is exactly the same thing you have there, hope it helps

Comment
Add comment · Show 5 · 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 Noztradamuz · Oct 23, 2013 at 06:20 PM 0
Share

Ohh I see... The On$$anonymous$$ouseUp function works on the collider of the Gameobject that have the Scrip attached, if you have a collider for the AOE of the turret make sure it's marked as trigger, and make sure is not on the main GameObject, because the On$$anonymous$$ouseUp function will trigger every time you move your mouse over that collider, one thing to solve this, is to put the AOE collider of the turret in a child GameObject inside the main turret GameObject and use another collider on the $$anonymous$$ain GO to activate the On$$anonymous$$ouseUp function.

Edit. Also you dont need to do raycasting when using On$$anonymous$$ouseUp and colliders, and if you want to show the Upgrades when you actually click over the turret it's better to use On$$anonymous$$ouseUpAsButton() function Documentation: On$$anonymous$$ouseUpAsButton

avatar image Theyrr · Oct 24, 2013 at 12:04 PM 0
Share

It does not get the enemies colliding the child gameobject. I have the main parent object, which is a cube, as a child it has a sphere (rotates to face targets) and that has the cannons as child objects. Does that affect the thing?

Image

asd.png (73.4 kB)
avatar image Noztradamuz · Oct 24, 2013 at 03:57 PM 0
Share

Ok. You can put the AOE inside the Cannon GameObject and attach a different script for the ShowUpgrades code inside the Sphere, you dont need to do everything in one single script, try to divide scripts by functionallity, so you can reuse them in another objects

avatar image Theyrr · Oct 24, 2013 at 05:36 PM 0
Share

Still does not trigger. Would you like me to post the project here if you'd like to observe it a bit more?

avatar image Klarax · Apr 08, 2014 at 05:52 PM 0
Share

i had this issue, changed my if(open){open = false;} and it works. Glad this thread exists..... !!!

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

16 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

Related Questions

Use OnMouseEnter/OnMouse Exit with center of screen 1 Answer

Raycast and GUI question 1 Answer

How to force refresh mouse position? 2 Answers

Translate Mouse to Worldspace 0 Answers

Ensuring Correct Call Order 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