- Home /
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<PositionSelector>();
_positionSelector.enabled = false;
_camera = Camera.main.GetComponent<SmoothLookAt>();
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<Unit>.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<Vector3>.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?--
Your answer
Follow this Question
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