• 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 /
avatar image
0
Question by mkobaner · Mar 25, 2016 at 09:08 PM · collision detection

OntriggerEnter, whatcan be the problem ?

Hello,

I try to change the direction of the object when it touches the trigger. Main object is a cube, with mesh and collider, "45degree" gameobject has rigidbody kinematic, no gravity and is also a trigger in collider. Yet nothing happens. Here is the code i wrote if i am doing something wrong please let me know. thanks in advance.

 using UnityEngine;
 using System.Collections;
 
 public class Control : MonoBehaviour {
 
 
     public float speed = 6.0F;
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetAxis("Horizontal")> 0)
             {
                 transform.Translate(Vector3.right * Time.deltaTime *speed );
                 
 
 
             }
 
 
             
         if (Input.GetAxis("Horizontal")< 0)
         {
             transform.Translate(-Vector3.right * Time.deltaTime*speed );
         }
             }
 
 
     void onTriggerEnter(Collider temas)
     {
         if (temas.gameObject.name == "45degree" && Input.GetAxis ("Horizontal") > 0) {
             transform.Rotate (45, 0, 0);
             Debug.Log("contact h bigger than 1");
         }
 
         if (temas.gameObject.name == "45degree" && Input.GetAxis ("Horizontal") < 0) {
             transform.Rotate (-45, 0, 0);
             Debug.Log("contact h smaller than 1");
         }
         if (temas.gameObject.name == "45degree") {
             transform.Rotate (0, 0, 0);
             Debug.Log("contact");
         }
     }
 
 }
 
Comment
Add comment · Show 1
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
avatar image TreyH · Mar 25, 2016 at 09:19 PM 2
Share

capitalize your OnTriggerEnter, first of all.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Fredex8 · Mar 25, 2016 at 09:31 PM

It should be void OnTriggerEnter(Collider temas). The lack of capitalisation prevents it from working.

However you should also fix a few other things in that code.

You currently have no condition for if the (Input.GetAxis("Horizontal") value is exactly 0. Also since <0 and >0 cannot be true at the same time you should use an else if statement to cut down on the number of conditions being run.

I also wouldn't recommend having if (temas.gameObject.name == "45degree") { three times under OnTriggerEnter. Firstly it is unnecessary and inefficient but also since you are not using else statements that last condition is always going to be true at the same time as one of the ones above it and hence cause the rotation to reset to 0 regardless of the horizontal input axis value

Change it to something like this:

     void OnTriggerEnter(Collider temas)
     {
         if (temas.gameObject.name == "45degree")
         {
             if (Input.GetAxis("Horizontal") > 0)
             {
                 transform.Rotate(45, 0, 0);
                 Debug.Log("contact h bigger than 1");
             }
             else if (Input.GetAxis("Horizontal") < 0)
             {
                 transform.Rotate(-45, 0, 0);
                 Debug.Log("contact h smaller than 1");
             }
             else
             {
                 //I'm not sure what you need this for but I assume if exactly 0?
                 //Either way the old version would not have worked and needed changing
                 transform.Rotate(0, 0, 0);
             }
         }
     }

You probably want to cache the (Input.GetAxis("Horizontal") value somewhere though rather than getting it again OnTriggerEnter. Doesn't really make sense to be constantly getting it under update but then call it again elsewhere rather than using those values.

Comment
Add comment · Show 3 · Share
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
avatar image mkobaner · Mar 25, 2016 at 09:40 PM 0
Share

Thanks ill test first thing in morning, the 3 if conditions were there for debug purpose only i was interested in getting the debuglogs but i should habe used else there.

avatar image TreyH · Mar 26, 2016 at 12:26 AM 0
Share

Why is this calling a rotation of 0 degrees in any direction?

avatar image mkobaner · Mar 26, 2016 at 12:31 AM 0
Share

s not the final code, i need to understand if trigger happened without the axis condition. Ill remove it after debugging.

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

39 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

Related Questions

How can I change the Unity 5 UI hitbox/collider to fit my complex image? 1 Answer

How the 3D object on target image collide with the 3D game object of UI? 0 Answers

I can't manage to detect my collisions 0 Answers

help to understand collision detection 1 Answer

how do i create scripts for creating question and answer system? 0 Answers


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