//This function runs for each collider on our trigger zone, on each frame they are on our trigger zone. void OnTriggerEnter(Collider other) { //If it hasn't been initiated by the player, just stop here. if (!Initiated) { return; } //Was an Owner set for us? It's probably an enemy, let's check, if it is we'll let him hit the player if (Owner) { //Am I the owner? If I am I don't want to hit myself so return if (Owner == other.gameObject) { return; } //I'm not the owner, but the owner is an enemy, so if the collider is a player we should... if (Owner.tag == "Enemy" && other.gameObject.tag == "Player") { if (other.attachedRigidbody) { //Deal Damage DoDamage.Attack(other.gameObject, ProjDamage, 0, 0); //push from Owner Enemy Vector3 pushDir = (other.transform.position - Owner.transform.position); pushDir.y = 0f; pushDir.y = PushHeight * 0.1f; if (other.GetComponent <Rigidbody> () && !other.GetComponent <Rigidbody> ().isKinematic) { other.GetComponent <Rigidbody> ().velocity = new Vector3(0, 0, 0); other.GetComponent <Rigidbody> ().AddForce(pushDir.normalized * PushForce, ForceMode.VelocityChange); other.GetComponent <Rigidbody> ().AddForce(Vector3.up * PushHeight, ForceMode.VelocityChange); } Destroy(this.gameObject); return; } } // prevent the bullet spawned by the enemy from immediately destroying the enemy } else { //If the object colliding doesn't have the tag player and is not a trigger... if (other.gameObject.tag != "Player" && !other.isTrigger) { //If it's a rigid body, tell our DealDamage to attack it! if (other.attachedRigidbody) { //This was the bit of code that was letting Enemys shoot other enemys, becasue it didn't have //the check to make sure the owner was the player if (Owner.tag == "Player") { DoDamage.Attack(other.gameObject, ProjDamage, PushHeight, PushForce); } } //If it isn't we still probably want to destroy our projectile since it has a collider, so destroy it wether it is a rigid body or not. Destroy(this.gameObject); } } }
IEnumerator TriggerMeleeAttack() { if (animatorController) { animatorController.SetTrigger("MeleeAttack"); dealDamage.Attack(attackTrigger.hitObject, attackDmg, pushHeight, pushForce); } yield return(0); }
public bool IsGrounded(float distanceToCheck, LayerMask groundMask) { for (var i = 0; i < floorCheckers.Length; i++) { groundInfo[i] = GroundChecking.GetGroundHitInfo(floorCheckers[i], distanceToCheck + groundCheckOffset, groundMask); } if (EnemyBounceHit() != null) { var enemyTransform = EnemyBounceHit().transform; currentEnemyAi = enemyTransform.GetComponent <EnemyAI>(); currentEnemyAi.BouncedOn(); dealDamage.Attack(enemyTransform.gameObject, 1, 0f, 0f); } slopeNormal = AverageContactNormal(); slopeAngle = Vector3.Angle(slopeNormal, Vector3.up); currentlyGrounded = PointsOfContact() != 0; if (currentlyGrounded && ResetCamOnGrounded) { Invoke(nameof(LockCamNow), camLockResetTime); } if (!currentlyGrounded) { CancelInvoke(nameof(LockCamNow)); } col.material = currentlyGrounded ? frictionPlayerMat : frictionlessPlayerMat; return(currentlyGrounded); }
//returns whether we are on the ground or not //also: bouncing on enemies, keeping player on moving platforms and slope checking private bool IsGrounded() { //get distance to ground, from centre of collider (where floorcheckers should be) float dist = GetComponent <Collider>().bounds.extents.y; //check whats at players feet, at each floorcheckers position foreach (Transform check in floorCheckers) { RaycastHit hit; if (Physics.Raycast(check.position, Vector3.down, out hit, dist + 0.05f)) { if (!hit.transform.GetComponent <Collider>().isTrigger) { //slope control slope = Vector3.Angle(hit.normal, Vector3.up); //slide down slopes if (slope > slopeLimit && hit.transform.tag != "Pushable") { Vector3 slide = new Vector3(0f, -slideAmount, 0f); rigid.AddForce(slide, ForceMode.Force); } //enemy bouncing if (hit.transform.tag == "Enemy" && rigid.velocity.y < 0) { enemyAI = hit.transform.GetComponent <EnemyAI>(); enemyAI.BouncedOn(); onEnemyBounce++; dealDamage.Attack(hit.transform.gameObject, 1, 0f, 0f); } else { onEnemyBounce = 0; } //moving platforms if (hit.transform.tag == "MovingPlatform" || hit.transform.tag == "Pushable") { //rigid.drag = 5.0f; movingObjSpeed = hit.transform.GetComponent <Rigidbody>().velocity; movingObjSpeed.y = 0f; //9.5f is a magic number, if youre not moving properly on platforms, experiment with this number rigid.AddForce(movingObjSpeed * 7.7f * Time.fixedDeltaTime, ForceMode.VelocityChange); animator.SetBool("M_Platform", true); ForceIdle(); } else { movingObjSpeed = Vector3.zero; animator.SetBool("M_Platform", false); } //yes our feet are on something return(true); } } } movingObjSpeed = Vector3.zero; //no none of the floorchecks hit anything, we must be in the air (or water) return(false); }
public void Attack() { if (!inBattle) { return; } dealDamage.Attack(); timer.RestartTimer(); }
//if were checking for a physical collision, attack what hits this object void OnCollisionEnter(Collision col) { if (!collisionEnter) { return; } foreach (string tag in effectedTags) { if (col.transform.tag == tag) { dealDamage.Attack(col.gameObject, damage, pushHeight, pushForce); if (hitSound) { GetComponent <AudioSource>().clip = hitSound; GetComponent <AudioSource>().Play(); } } } }
//if were checking for a physical collision, attack what hits this object void OnCollisionEnter(Collision col) { if (!collisionEnter) { return; } foreach (string tag in effectedTags) { if (col.transform.tag == tag) { if (InstaDestroy) { if (col.transform.tag == "Player" || col.transform.tag == "Pushable" || col.transform.tag == "pickup") { p_Health = col.gameObject.GetComponent <PlayerHealth>(); p_Health.InstantRespawn = true; col.gameObject.transform.position = p_Health.respawnPos; //p_Health.Death(); } else { dealDamage.Attack(col.gameObject, 9999, pushHeight, pushForce); } } else { dealDamage.Attack(col.gameObject, damage, pushHeight, pushForce); } if (hitSound) { aSource.clip = hitSound; aSource.Play(); } if (col.transform.tag != "Player") { dealDamage.Attack(col.gameObject, damage, pushHeight, pushForce); } } } }
void Update() { if (sightTrigger && sightTrigger.colliding && chase) { characterMotor.MoveTo(sightTrigger.hitObject.transform.position, acceleration, chaseStopDistance, ignoreY); } if (attackTrigger && attackTrigger.collided) { dealDamage.Attack(attackTrigger.hitObject, attackDmg, pushHeight, pushForce); } }
void Update() { //chase if (sightTrigger && sightTrigger.colliding && chase) { characterMotor.MoveTo(sightTrigger.hitObject.transform.position, acceleration, chaseStopDistance, ignoreY); //nofity animator controller if (animatorController) { animatorController.SetBool("Moving", true); } //disable patrol behaviour if (moveToPointsScript) { moveToPointsScript.enabled = false; } } else { //notify animator if (animatorController) { animatorController.SetBool("Moving", false); } //enable patrol behaviour if (moveToPointsScript) { moveToPointsScript.enabled = true; } } //attack if (attackTrigger && attackTrigger.collided) { dealDamage.Attack(attackTrigger.hitObject, attackDmg, pushHeight, pushForce); //notify animator controller if (animatorController) { animatorController.SetBool("Attacking", true); } } else if (animatorController) { animatorController.SetBool("Attacking", false); } }
//This function runs for each collider on our trigger zone, on each frame they are on our trigger zone. void OnTriggerEnter(Collider other) { //If it hasn't been initiated by the player, just stop here. if (!Initiated) { return; } //If the object colliding doesn't have the tag player and is not a trigger... if (other.gameObject.tag != "Player" && !other.isTrigger) { //If it's a rigid body, tell our DealDamage to attack it! if (other.attachedRigidbody) { DoDamage.Attack(other.gameObject, ProjDamage, ProjHeight, ProjForce); } //If it isn't we still probably want to destroy our projectile since it has a collider, so destroy it wether it is a rigid body or not. Destroy(this.gameObject); } }
//This function runs for each collider on our trigger zone, on each frame they are on our trigger zone. void OnTriggerStay(Collider other) { //If we're not punching, forget about it, just stop right here! if (!Punching) { return; } //If we are punching, and the tag on our trigger zone has a RigidBody and it's not tagged Player then... if (other.attachedRigidbody && other.gameObject.tag != "Player") { //If this guy on our trigger zone is not on our List of people already punched with this punch if (!BeingPunched.Contains(other.gameObject)) { //Call the DealDamage script telling it to punch the hell out of this guy DoDamage.Attack(other.gameObject, PunchDamage, PushHeight, PushForce); //Add him to the list, so we won't hit him again with the same punch. BeingPunched.Add(other.gameObject); } } }
void Update() { if (sightTrigger && sightTrigger.colliding && chase && sightTrigger.hitObject != null && sightTrigger.hitObject.activeInHierarchy) { transform.LookAt(sightTrigger.hitObject.transform); characterMotor.MoveTo(sightTrigger.hitObject.transform.position, acceleration, chaseStopDistance, ignoreY); if (animatorController) { animatorController.SetBool("Moving", true); } wander = false; } else { if (animatorController) { animatorController.SetBool("Moving", false); } wander = true; } if (wander) { transform.LookAt(targetPosition); characterMotor.MoveTo(targetPosition, acceleration, wanderStopDistance, ignoreY); } if (attackTrigger && attackTrigger.collided) { dealDamage.Attack(attackTrigger.hitObject, attackDmg, pushHeight, pushForce); if (animatorController) { animatorController.SetBool("Attacking", true); } } else if (animatorController) { animatorController.SetBool("Attacking", false); } }
//returns whether we are on the ground or not //also: bouncing on enemies, keeping player on moving platforms and slope checking private bool IsGrounded() { //get distance to ground, from centre of collider (where floorcheckers should be) float dist = GetComponent <Collider>().bounds.extents.y; //check whats at players feet, at each floorcheckers position int connectingRays = 0; Vector3 totalNormal = Vector3.zero; int loopCount = 0; foreach (Transform check in floorCheckers) { RaycastHit hit; if (Physics.Raycast(check.position, Vector3.down, out hit, dist + 0.05f, mask)) { //Debug.DrawRay(check.position, Vector3.down, Color.green); if (!hit.transform.GetComponent <Collider>().isTrigger) { //slope control //slopeNormal = hit.normal.normalized; totalNormal += hit.normal.normalized;; slope = Vector3.Angle(hit.normal, Vector3.up); //slide down slopes if (slope > slopeLimit && hit.transform.tag != "Pushable") { Vector3 slide = new Vector3(0f, -slideAmount, 0f); rigid.AddForce(slide, ForceMode.Force); } //enemy bouncing if (hit.transform.tag == "Enemy" && rigid.velocity.y < 0) { enemyAI = hit.transform.GetComponent <EnemyAI>(); enemyAI.BouncedOn(); onEnemyBounce++; dealDamage.Attack(hit.transform.gameObject, 1, 0f, 0f); } else { onEnemyBounce = 0; } //moving platforms if (hit.transform.tag == "MovingPlatform" || hit.transform.tag == "Pushable") { movingObjSpeed = hit.transform.GetComponent <Rigidbody>().velocity; movingObjSpeed.y = 0f; //9.5f is a magic number, if youre not moving properly on platforms, experiment with this number rigid.AddForce(movingObjSpeed * movingPlatformFriction * Time.fixedDeltaTime, ForceMode.VelocityChange); } else { movingObjSpeed = Vector3.zero; } //yes our feet are on something //return true; connectingRays++; if (loopCount == 5) { specificSlopeNormal = hit.normal; } } } else { Debug.DrawRay(check.position, Vector3.down, Color.red); } loopCount++; } float nX = Mathf.Abs(totalNormal.x) > 0 ? totalNormal.x / connectingRays : 0; float nY = Mathf.Abs(totalNormal.y) > 0 ? totalNormal.y / connectingRays : 0; float nZ = Mathf.Abs(totalNormal.z) > 0 ? totalNormal.z / connectingRays : 0; slopeNormal = new Vector3(nX, nY, nZ); float sX = Mathf.Abs(specificSlopeNormal.x) > 0 ? specificSlopeNormal.x / 2 : 0; float sY = Mathf.Abs(specificSlopeNormal.y) > 0 ? specificSlopeNormal.y / 2 : 0; float sZ = Mathf.Abs(specificSlopeNormal.z) > 0 ? specificSlopeNormal.z / 2 : 0; // specificSlopeNormal = new Vector3(sX, sY, sZ); movingObjSpeed = Vector3.zero; //no none of the floorchecks hit anything, we must be in the air (or water) if (connectingRays == 0) { return(false); } else { return(true); } }
public void SomethingEnteredTrigger(Collider other) { print("hit " + other.transform); MoveBase.movementStateMachine.NormalMovement(); playerDealDamage.Attack(gameObject, 0, crashForce.y, crashForce.x, other.ClosestPoint(transform.position)); }