• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Shindy117 · Jun 04, 2017 at 09:58 PM · rigidbodymathf.clamp

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

capture.png (29.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem dealing with colliders in a complex project [SOLVED] 0 Answers

Animator prevents rigidbody movement 1 Answer

Multiplayer Ragdoll not synchronizing! 1 Answer

Rigidbody.isKinematic teleporting to ground after toggle 0 Answers

AI detect incoming rigid body position placement to catch it 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges