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
1
Question by stetsmando · Jun 03, 2015 at 05:23 AM · c#mouseposition2d rotationonmouseenter

Unable to Update Multiple GameObjects Together

Hello!

I'm new to Unity, but have some experience with C#.

In order to familiarize myself with Unity I've decided to create a simple top-down tower defense game.

Currently I'm working on the TowerController script that will allow the towers to pivot and follow enemies as they enter a given radius. I've figured out how to handle the triggering within a radius and have the tower rotate and face a target, however, when I have multiple towers placed in the scene they don't respond at the same time. Currently I'm using the MousePosition as the the target for the towers. In the future I will be using other gameObjects. It appears that the towers cannot trigger at the same time. I'm not sure why this is and any feedback would be greatly appreciated.

Below is a screenshot of the scene:

alt text

Here is a screenshot of the tower GameObject:

alt text

Finally here is the TowerController.cs script that I'm using:

 using UnityEngine;
 using System.Collections;
 
 public class TowerManager : MonoBehaviour {
 
     private bool _tracking = false;
 
     
     void OnMouseEnter() {
         _tracking = true;
         Debug.Log (gameObject.name + " tracking mouse");
     }
 
     void OnMouseExit() {
         _tracking = false;
     }
 
     void Update () {
         if (_tracking) {
             var mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             Quaternion rot = Quaternion.LookRotation (transform.position - mousePos, Vector3.forward);
             gameObject.transform.rotation = rot;
         }
     }
 }
 
 
 
 

scene.png (16.1 kB)
tower.png (37.0 kB)
Comment
Add comment · Show 2
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 Oribow · Jun 03, 2015 at 07:28 PM 0
Share

On which GameObject is the script? Because "On$$anonymous$$ouseEnter" is only called, if the $$anonymous$$ouse is above the GameObject, the script is attached to.

avatar image stetsmando · Jun 03, 2015 at 07:36 PM 0
Share

@quest-23
There is a copy of the TowerController.cs script on all 4 towers.

On$$anonymous$$ouseEnter seems to trigger when in range of my Circle Collider 2d. (Which is the effect I'm going for as the towers will target other objects in the given range of their circle collider)

I tested this with 1 tower and the functionality works great, it's when I try and have multiple towers that I run into the issue of only 1 of the towers targeting at a time. Even if the mouse is in the circle collider of more than 1 tower.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by neonblitzer · Jun 03, 2015 at 07:38 PM

The mouse can only be on one object, i.e. only the "topmost" object's OnMouseEnter is called. Its collider blocks the rest. You should probably store the target position (mouse position in this case) somewhere and then have each tower check the distance to it to determine if they are close enough to start tracking it.

Comment
Add comment · Show 1 · 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 stetsmando · Jun 03, 2015 at 07:42 PM 1
Share

@neonblitzer

That's actually what I was figuring the issue to be, but wasn't sure.
Ultimately I'm not going to have the mouse be the target object, I just wanted it for testing purposes.
I'm going to add in a basic enemy object and verify that multiple towers will react to it's collider.
Thanks for your response!

avatar image
1

Answer by Oribow · Jun 03, 2015 at 07:46 PM

I recomend you to use another aproach:

         Vector3 ownPosition = transform.position;
         ownPosition.y = 0;
         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         mousePos.y = 0;
         float distance = Vector3.Distance(ownPosition,mousePos);
         if (distance <= range)
         {
             transform.LookAt(mousePos);
         }

This script simply calculate the distance between the tower and the mouse. (Ignoring the height(y)) If the distance is lower then the range of the tower, the tower will start to lookat the mouse.

Comment
Add comment · Show 1 · 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 stetsmando · Jun 04, 2015 at 05:26 AM 0
Share

@quest-23

Thank you so much for this! I'm actually not going to use the mouse long term (as this is going to be a tower defense game). I'm sure I'll use this for something in the future though!

Thanks again!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

[Solved]MouseEnter not working! 1 Answer

How to make a 3D tank turret follow the DIRECTION of mouse cursor? 1 Answer

How do I spawn a prefab where I click? 2 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