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 Disaster · Apr 27, 2011 at 10:50 PM · inputupdate

Update - Delaying until next frame?

I am trying to make a mechanic similar to that of an RTS. If you click a unit, that unit becomes selected, then when you click on the terrain the unit will move to that position.

I have two classes, my UnitSelector class and my PositionSelector. One first handles the selection of units and the other handles the clicking of a position on the terrain. I also have a manager class which controls both of these classes together, here is my code:

private UnitSelector _unitSelector; private PositionSelector _positionSelector; private SmoothLookAt _camera; void Start () { _unitSelector = gameObject.GetComponent<UnitSelector>(); _unitSelector.enabled = true;

 _positionSelector = gameObject.GetComponent&lt;PositionSelector&gt;();
 _positionSelector.enabled = false;

 _camera = Camera.main.GetComponent&lt;SmoothLookAt&gt;();

 addEventListeners();

}

void addEventListeners() { Messenger<Unit>.AddListener(UnitSelector.UNIT_SELECTED, onUnitSelected); Messenger<Vector3>.AddListener(PositionSelector.POSITION_CHANGED, OnPositionChange); }

void onUnitSelected(Unit u) {
_camera.target = u.transform; _positionSelector.enabled = true; } void OnPositionChange(Vector3 p) { if(!_unitSelector.selectedUnit) return;

 _unitSelector.selectedUnit.SetDestination(p);

}

UnitSelector update method:

public Unit selectedUnit { get { return _selectedUnit; } set { if(_selectedUnit) { _selectedUnit.renderer.material.color = Color.white; } _selectedUnit = value;

             _selectedUnit.renderer.material.color = Color.red;

             Messenger&lt;Unit&gt;.Broadcast(UnitSelector.UNIT_SELECTED, _selectedUnit, MessengerMode.DONT_REQUIRE_LISTENER);
         }
     }

void Update () { if(Input.GetButtonDown("Fire1")){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray, out hit, 1000F, _layerMask.value)){ Unit clickedUnit = hit.collider.GetComponent<Unit>(); if(clickedUnit) selectedUnit = clickedUnit;

                 }
             }
         }

PositionSelector update method:

void Update () { if(Input.GetButtonUp("Fire1")){ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray, out hit, 1000F, _layerMask.value)){

             // Store the current position
             _currentPos = hit.point;

             // Broadcast the position change
             Messenger&lt;Vector3&gt;.Broadcast(POSITION_CHANGED, _currentPos, MessengerMode.DONT_REQUIRE_LISTENER);
         }
     }
 }

The problem here is that if I select a unit, then select another, the PositionSelector click event is firing too causing an instant movement. I'm trying to figure out a way delaying the PositionSelector click so it is not registered when clicking between units.

To try and simplify what I am after, here is what is currently happening:

Unit clicked
Position selector enabled
Terrain clicked
Position selector dispatched event
Unit moves to position
- Works as intended -

Unit clicked
Position selector enabled
Another unit clicked
Position selector dispatched event
Unit moves to position even though we technically haven't clicked the terrain
- Wrong! -

Does anyone have any bright ideas as to how I can get my PositionSelector to only 'enable' a frame after a unit is selected?--

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

0 Replies

· Add your reply
  • Sort: 

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

No one has followed this question yet.

Related Questions

Input versus Event 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Why am I losing some inputs on Update function? 0 Answers

Eliminating input loss 1 Answer

How to Count an Input, key press on keyboard 1 Answer


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