Mathf.clamp cancels my trigger
Hey guys so I have a mathf.clamp canceling my trigger code. So in my game I have enemy objects that are suppose to be destroyed by my Mesh Collider trigger when my ship comes in contact with them. However what I found out is my Clamp code is canceling my OnTriggerEnter game code.
My clamp code makes it so I cant leave the play area.
Things to know: all the objects are level with each other. And my mesh collider is checked as "Is Trigger"
As seen below: ![alt text][1]
Here is my code also:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour {
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
public Boundary boundary;
private Rigidbody rb;
private AudioSource audioFire;
private float nextFire;
void Awake()
{
audioFire = GetComponent<AudioSource>();
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
audioFire.Play();
}
}
void FixedUpdate()
{
rb.position = new Vector3
(Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax));
}
}
Player destroy enemies code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerResource : MonoBehaviour {
public int PlayerHP;
public int PlayerShield;
public Slider Health;
// Use this for initialization
void Start () {
PlayerHP = 100;
PlayerShield = 0;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Meteor(Clone)") {
PlayerHP = PlayerHP - 5;
Health.value = PlayerHP;
}
else if (other.gameObject.name == "enemyFireball(Clone)")
{
Destroy(other.gameObject);
PlayerHP = PlayerHP - 25;
Health.value = PlayerHP;
}
else if (other.gameObject.name == "Meteor2(Clone)")
{
Destroy(other.gameObject);
PlayerHP = PlayerHP - 5;
Health.value = PlayerHP;
}
else if (other.gameObject.name == "RegularSpider(Clone)")
{
Destroy(other.gameObject);
PlayerHP = PlayerHP - 10;
Health.value = PlayerHP;
}
else if (other.gameObject.name == "MutatingSpider(Clone)")
{
Destroy(other.gameObject);
PlayerHP = 0;
Health.value = PlayerHP;
}
else if(other.gameObject.name == "Debris(Clone)")
{
PlayerHP = PlayerHP - 5;
Health.value = PlayerHP;
}
//-------------------------------------------------------------------------
}
}
SO why is my clamp code affecting the mesh collider and what are some solutions to get my code working? [1]: /storage/temp/95372-capture.png
Your answer
Follow this Question
Related Questions
Making a connection point system like robocraft 0 Answers
My Enemies walk through objects and my FPS character model 3 Answers
rigidbody iskinematic issue 1 Answer
Physics relative to parent 1 Answer
How to Make A Character Stop At Wall? 0 Answers