First of all, I'm sorry about the lack of information in the title. but I couldn't do better.
The point is I builded an abstract class who checks all my collisions, inside there is a public bool called "isInteracting" to check if I'm inside a collider, if I do "isInteracting" is true, otherwise false. So, classes who inherits from this parent class can use "isInteracting" to knows when I'm inside the collider an do something (ex. display a message, play a sound, etc.), when I created the second class who inherits from the parent class I realized my bool to check if I'm inside the collider didn't work fine because both child classes call it, How can I fix it? make them independents.
Simple Example:
public class Waitress : CollisionBehaviour
{
public override void SayHi()
{
if (isInteracting) Debug.Log("Hi");
else return;
}
public override void Update()
{
SayHi();
base.Update();
}
}
"SayHi()" is an abstract method from the abstract class "CollisionBehaviour", and "Update()" is overridden because it's virtual.
At the request of #Poul Bak:
Parent Class:
public abstract class CollisionBehaviour : MonoBehaviour
{
[HideInInspector] public bool isInteractingByButtonAction;
[HideInInspector] public bool isInteractingByTriggerCollision;
private bool buttonPressed, isInside, a, b;
private void CheckStay()
{
isInteractingByTriggerCollision = true;
// GameInputs.Interact is a mapped button from my controller
if (GameInputs.Interact && !buttonPressed)
{
buttonPressed = true;
isInteractingByButtonAction = true;
}
a = (!buttonPressed) ? true : false;
b = (!buttonPressed) ? true : false;
}
private void CheckExit()
{
isInside = false;
b = false;
isInteractingByButtonAction = false;
isInteractingByTriggerCollision = false;
if (buttonPressed) buttonPressed = false;
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player")) isInside = true;
}
private void OnTriggerExit(Collider other)
{
CheckExit();
}
private void Update()
{
if (isInside) CheckStay();
}
public virtual void LateUpdate()
{
if (a && b) { /* Will display a sprite to indicate which button should be pressed*/ }
}
}
Child class:
public class InteractableObject : CollisionBehaviour
{
#region Exposed Variables
public AnimationCurve ease;
public float smoothTime;
public float minFov = 30;
public float maxFov = 50;
#endregion
private float currentVelocity;
private void InteractionFieldOfView()
{
// Animerp is an animation curve interpolation... Clever name I know
Camera.main.fieldOfView = isInteractingByTriggerCollision.Animerp(maxFov, minFov, ref currentVelocity, smoothTime, ease);
}
public override void LateUpdate()
{
InteractionFieldOfView();
base.LateUpdate();
}
}
Well, camera field of view (isInteractingByTriggerCollision) only works fine when only one class inherit from CollisionBehaviour, as I told you.
BTW, I avoid OntriggerStay because garbage collector is pretty high
Your post is poorly formatted and explained and I found it hard to understand what you want your code to do an the errors being faced, nevertheless I took a shot.
This class should replace your CollisionBehaviour class, it essentially imitates OnTriggerStay by calling a function every frame while the player is within the trigger.
using UnityEngine;
[RequireComponent(typeof(SphereCollider))]
internal abstract class CollisionTrigger : MonoBehaviour
{
private bool _isPlayerInsideTrigger = false;
private void Update()
{
if(_isPlayerInsideTrigger)
{
FakeOnTriggerStay();
}
}
private void OnTriggerEnter(Collider collider)
{
if(!collider.CompareTag("Player")) return;
_isPlayerInsideTrigger = true;
}
public abstract void FakeOnTriggerStay();
private void OnTriggerExit(Collider collider)
{
if(!collider.CompareTag("Player")) return;
_isPlayerInsideTrigger = false;
}
}
This is an example to demonstrate what your Waitress class would look like using the provided class above.
internal class Waitress : CollisionTrigger
{
public override void FakeOnTriggerStay()
{
// Replace this with input system you are using.
if(Input.GetKeyDown(...))
{
Debug.Log("Hi");
}
}
}
Hope this helps.
Related
So I'm new to C# I somewhat know Python I couldn't understand how functions work I tried doing something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collisiondetectorleft : MonoBehaviour
{
public class Triggerdetecting()
{
public void OnTriggerStay(Collider other)
{
if (other.attachedRigidbody)
other.attachedRigidbody.AddForce((Vector3.up * 10);
}
}
void FixedUpdate()
{
if (Input.GetKeyDown("space"))
{
//I'm so lost
Triggerdetecting objTriggerdetecting = new Triggerdetecting();
}
}
}
I'm trying to create some sort of hitbox by detecting trigger if a button pressed and meets the condition make the object more faster. I tried few ways to call function non of them worked. Thank you for your time. If you unable to understand what I meant you can ask me I'll try to explain in other ways.
Want something like this:
def detection():
if OnTriggerStay == True:
moveobject up
if Input.GetKeyDown("space")) == True:
detection()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collisiondetectorleft : MonoBehaviour
{
//create bool to set it true or false while inside the object
private bool _canHit;
private void Update()
{
if (_canHit)
{
if (Input.GetKeyDown(KeyCode.A))
{
_canHit = false;
//task
}
}
}
//set _canHit true if object enters trigger
private void OnTriggerEnter(Collider other)
{
if (other.attachedRigidbody)
_canHit = true;
}
//set _canHit false if object exits trigger
private void OnTriggerExit(Collider other)
{
if (other.attachedRigidbody)
_canHit = false;
}
}
I was able to detect and log while true but I didn't figured how to react to object that triggers
I would go the same route as you did in your answer attempt.
The last bit missing in order to b able to actually use the according object is simple: Instead of a bool rather directly store the object
public class collisiondetectorleft : MonoBehaviour
{
private Collider _currenTarget;
private void Update()
{
if (_currenTarget && Input.GetKeyDown(KeyCode.A))
{
Debug.Log($"I am interacting with {_currentTarget}");
_currenTarget = null;
}
}
private void OnTriggerEnter(Collider other)
{
if (!other.attachedRigidbody) return;
_currenTarget = other;
}
private void OnTriggerExit(Collider other)
{
if (other != _currenTarget) return;
_currenTarget = null;
}
}
These two blocks of code I assume are involved with the following error:
Assets\Scripts\Weapons.cs(8,12): error CS7036: There is no argument given that corresponds to the required formal parameter 'name' of 'Item.Item(string, int)'
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Item : MonoBehaviour
{
private string name;
private int quantity;
public Item(string name, int quantity)
{
this.name = name;
this.quantity = quantity;
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Quantity
{
get { return quantity; }
set { quantity = value; }
}
public virtual void UseItem()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapons : Item
{
public GameObject bullet;
public GameObject shotgunSpawn;
public GameObject shotgunSpawn2;
//public bool useGun;
public Inventory iw;
GUI_2D m;
public float Speed;
GameObject patroller;
GameObject guard;
public bool pellet;
public bool shotGun;
public bool barGun;
public PlayerController player;
// Start is called before the first frame update
void Start()
{
Speed = 5f;
player = GameObject.FindGameObjectWithTag("Player");
patroller = GameObject.FindGameObjectWithTag("Patroller");
guard = GameObject.FindGameObjectWithTag("Guard");
guard = GameObject.FindGameObjectWithTag("Shotgun");
pellet = false;
shotGun = false;
barGun = false;
}
void DestroyEnemy()
{
if (patroller)
{
patroller.SetActive(false);
}
if (guard)
{
patroller.SetActive(false);
}
}
private void OnTriggerEnter(Collider other)
{
if (iw.invItems.Count < 12)
{
if (other.gameObject.CompareTag("Player"))
{
pellet = true;
}
}
}
public override void UseItem()
{
if (pellet)
{
player.pellet = true;
player.shotGun = false;
player.barGun = false;
}
if (shotGun)
{
player.pellet = false;
player.shotGun = true;
player.barGun = false;
}
if (barGun)
{
player.pellet = false;
player.shotGun = false;
player.barGun = true;
}
base.UseItem();
}
}
I am not trying to change the item class since doing so will affect another class that depends on the item classes constructor. Unless there is another way to resolve the error by changing the item class. The error given is also affecting another class in the same way. I am hoping to solve the issue in both the weapon class and the other class from an answer here. Thank you in advance.
You shouldn't add constructors to MonoBehaviours, as the Unity Engine is responsible for instantiating them and will never pass your constructors arguments (you shouldn't be creating instances of MonoBehaviours, either).
Just remove your Item constructor and assign the values manually whenever needed. Other construction code should be done in Awake() or Start().
In Unity:
Could anybody tell me what I did wrong. I wanted to pop up a dialogue after you are nearby a charecter but somehow my code doesn't really work.
public class Interactable : MonoBehaviour {
[HideInInspector]
public NavMeshAgent playerAgent;
private bool hasInteracted;
public virtual void MoveToIneraction(NavMeshAgent playerAgent)
{
hasInteracted = false;
this.playerAgent = playerAgent;
playerAgent.stoppingDistance = 2.3f;
playerAgent.destination = this.transform.position;
Interact ();
}
void Update()
{
if (!!hasInteracted && playerAgent != null && playerAgent.pathPending)
{
if(playerAgent.remainingDistance <= playerAgent.stoppingDistance)
{
Interact();
hasInteracted = true;
}
}
}
public virtual void Interact()
{
Debug.Log("Interacted");
}
}
!!hasInteracted
It should be !hasInteracted, I guess
I'm started with Unity, and I need (before game is in paused) trigger an avent. Is there any way to trigger any event when the Time.timeScale is changed to 0? Something like: Time.timeScale.onBeforeChange()...
Thanks a lot.
Make the thing that changes the time scale a controller, then make that controller raise the event.
[System.Serializable]
public class BeforeTimeChangedData
{
public bool canceled;
public float oldValue;
public float newValue;
}
[System.Serializable]
public class BeforeTimeChangedEvent : UnityEvent<BeforeTimeChangedData>
{
}
//Attach this to a game object that gets loaded in your pre-load scene with the tag "Singletons"
public class TimeController : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(gameObject);
if(BeforeTimeChanged == null)
BeforeTimeChanged = new BeforeTimeChangedEvent();
}
public BeforeTimeChangedEvent BeforeTimeChanged;
public bool ChangeTimeScale(float newValue)
{
var args = new BeforeTimeChangedData();
args.oldValue = Time.timeScale;
args.newValue = Time.timeScale;
BeforeTimeChanged.Invoke(args);
if(!args.canceled)
{
Time.timeScale = newValue;
}
return args.canceled;
}
}
Elsewhere you can change the timescale by doing
public class TimeSlower : MonoBehaviour
{
private TimeController _timeController;
public Text TimeChanged;
void Start()
{
var singletons = GameObject.FindWithTag("Singletons");
_timeController = singletons.GetComponent<TimeController>();
if(_timeController == null)
throw new System.ArgumentNullException("Could not find a TimeController on the Singletons object");
}
void Update()
{
if(Input.GetButton("SlowTime"))
{
var changed = _timeController.ChangeTimeScale(0.5f);
if(changed)
{
TimeChanged.text = "Time Changed!";
}
}
}
}
And here is another component that listens for the change and cancels the change if the change has happened too recently;
public class TimeChangeLimiter : MonoBehaviour
{
private float lastTimeChange = 0;
private TimeController _timeController;
public Text TimeChanged;
[Range(0, float.MaxValue)]
public float Cooldown;
void Start()
{
var singletons = GameObject.FindWithTag("Singletons");
_timeController = singletons.GetComponent<TimeController>();
if(_timeController == null)
throw new System.ArgumentNullException("Could not find a TimeController on the Singletons object");
_timeController.BeforeTimeChanged.AddListener(OnBeforeTimeChanged);
}
void OnDestroy()
{
_timeController.BeforeTimeChanged.RemoveListener(OnBeforeTimeChanged);
}
void OnBeforeTimeChanged(BeforeTimeChangedData args)
{
if(Time.time - lastTimeChange < Cooldown)
{
args.canceled = true;
return;
}
lastTimeChange = Time.time;
}
}
I have a list of enemys. so i want each enemy have their turn.
First of all :
Player turn --> enemys turn ("here each enemy move one by one untill the end then player move again"). how do i making some waiting time here and forcus on enemy turn?
Any help would be appreciated.
void Start()
{
// find list enemy
enemy = GameObject.FindGameObjectsWithTag("Enemy");
}
void Update()
{
//enemy turn reference to player. after move all enemy we change it to false to change the player turn.
if(StaticClass.enemyTurn == true )
{
for(int i=0;i<enemy.length;i++)
{
// how do i making some waiting time here and forcus on enemy turn?
EnemyTurn(i);
}
}
}
public void EnemyTurn(int id)
{
ChessMoveMent chessMoveScript = enemy[id].GetComponent<ChessMoveMent>();
chessMoveScript.ProcessMove();
id++;
if(id>=enemy.Length)
{
isMove = false;
}
}
I usually use StartCoroutine in this case.
Please try the below code:
public IEnumerator EnemyTurn(int id)
{
yield return null;
ChessMoveMent chessMoveScript = enemy[id].GetComponent<ChessMoveMent>();
chessMoveScript.ProcessMove();
id++;
if(id>=enemy.Length)
{
isMove = false;
}
}
When you want to use it, please use with "StartCoroutine()"
StartCoroutine(EnemyTurn(i));
More details here
You might have a coordinator, who tells the participants when it's their turn.
public class GameCoordinator : MonoBehaviour
{
public List<Participant> participants;
private int currentParticipantIdx = -1;
private Participant CurrentParticipant
{
get { return participants[currentParticipantIdx]; }
}
private void Start()
{
PlayNextMove();
}
private void PlayNextMove()
{
ProceedToNextParticipant();
CurrentParticipant.OnMoveCompleted += OnMoveCompleted;
CurrentParticipant.BeginMove();
}
private void OnMoveCompleted()
{
CurrentParticipant.OnMoveCompleted -= OnMoveCompleted;
StartCoroutine(PlayNextMoveIn(2.0f));
}
private IEnumerator PlayNextMoveIn(float countdown)
{
yield return new WaitForSeconds(countdown);
PlayNextMove();
}
private void ProceedToNextParticipant()
{
++currentParticipantIdx;
if (currentParticipantIdx == participants.Count)
currentParticipantIdx = 0;
}
}
public class Participant : MonoBehaviour
{
public event Action OnMoveCompleted;
public void BeginMove()
{
//
}
}