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");
		}
	}

}

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.