So in this unity project I have created a interaction to glorify that which counter I am currently facing so depending on that I can spawn related items from it but the game object should only Console “Selected” if I am looking towards it only but it’s happening Even if I am not looking towards it or is far away from it after first enabling it, it even sometimes becomes disabled when I face the counter again which is totally against the script I just wrote So any kind of help will be much appreciated.
Interactions Function
private void HandleInteractions()
{
Vector2 inputVector = gameInput.GameMovementVectorNormalized();
//This Transform relates to the actual transfor of the Project Asset aur in this case Player
Vector3 movDir = new Vector3(inputVector.x, 0f, inputVector.y);
if (movDir != Vector3.zero)
{
lastInteractDir = movDir;
}
float interactDistance = 1f;
//This method will detect collision and will return the collision objects reference too for interaction purposes
//Here we used the lastInteractDir to even if if stopped moving towards and interactable object
//we still get that objects reference to use it
if (Physics.Raycast(transform.position, lastInteractDir, out RaycastHit raycastHit, interactDistance, countersLayerMask))
{
if (raycastHit.transform.TryGetComponent(out ClearCounter clearCounter))
{
//Has Clear Counter
//clearCounter.Interact();
if (clearCounter != selectedCounter)
{
SetSelectedCounter(clearCounter);
//Here
}
else
{
//something in front but doesn't has clear counter script
SetSelectedCounter(null);
//Here
}
}
else
{
//if nothing infront
SetSelectedCounter(null);
//Here
}
}
//can use RaycastAll() to cycle through many possible objects to get interactions
//or can use the Raycast(15) to use the layers mask method and handle interactions
//based on Layers
}
Invoke Function
private void SetSelectedCounter(ClearCounter selectedCounter)
{
//this funtion so we dont have to duplicate code again and agian in the usage areas
this.selectedCounter = selectedCounter;
onSelectedCounterChanged?.Invoke(this, new onSelectedCounterChangedEventArgs
{
//making cross interaction between for selected counter and Clear counter per say
selectedCounter = selectedCounter
});
}
Event
public event EventHandler<onSelectedCounterChangedEventArgs> onSelectedCounterChanged;
public class onSelectedCounterChangedEventArgs : EventArgs
{
//This is the proper convention for Event Creation
public ClearCounter selectedCounter;
}
Interaction Function
private void GameInput_OnInteractAction(object sender, System.EventArgs e)
{
if (selectedCounter != null)
{
//if counterinfront is not null then Interation can be made
selectedCounter.Interact();
}
}
Activity Handler
private void Start()
{
Player.Instance.onSelectedCounterChanged += Player_onSelectedCounterChanged;
}
private void Player_onSelectedCounterChanged(object sender, Player.onSelectedCounterChangedEventArgs e)
{
if (e.selectedCounter == clearCounter)
{
Show();
}
else
{
Hide();
}
}
private void Show()
{
visualGameObject.SetActive(true);
}
private void Hide()
{
visualGameObject.SetActive(false);
}
Interact Function Logging
public void Interact()
{
Debug.Log("Interact!");
}
Thanks a bunch!
USMAN GHAUSE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.