MrRoss
MrRoss

Reputation: 35

Collision trigger always seems to be colliding

All I can find through research is objects NOT colliding... My problem is it is ALWAYS colliding... I have messed this code up all over trying to make it recognize that it is not colliding. They are not touching anything at all in unity ( cube triggers using box colliders )... I am kind of new to unity and I feel like I am over looking something simple?

using UnityEngine;
using System.Collections;

public class Battlecam : MonoBehaviour 
{

//calling speed integers
    public float upSpeed = 10;
    public float rightSpeed = 60;
//bool colliding
    public bool colliding = false;


//triggers
    void OnTriggerEnter(Collider other)
    {
        colliding = true;
    }


    void OnTriggerExit(Collider other)
    {
        colliding = false;
    }


    void Start()
    {
            colliding = false;
    }
//camera movements
    void Update()
        {
            if (colliding = false) {
                float horizontal = Input.GetAxis ("Horizontal") * rightSpeed * Time.deltaTime;
                transform.Translate (0, horizontal, 0);

                float vertical = Input.GetAxis ("Vertical") * upSpeed * Time.deltaTime;
                transform.Translate (0, 0, vertical);

                colliding = false;
            } 
            else
                colliding = true;
        }

}

I can't figure out what is throwing my bool to true. I have scoured google and stack exchange for any answer, with little luck. Thanks

Upvotes: 1

Views: 168

Answers (2)

ʇolɐǝz ǝɥʇ qoq
ʇolɐǝz ǝɥʇ qoq

Reputation: 734

Dinal24 had the first point.

The other thing you should fix is:
Change OnTriggerEnter(Collider other) to OnTriggerStay(Collider other).
Why?
If the program starts when the the object is already touching(bool should return true), it is forced to return false because of:

void Start()
{
    colliding = false;
}

One option is, if you use OnTriggerStay, it will contantly detect if it's true or not.
Another option is to delete the void Start() completely. I don't think there is any need for it.

Upvotes: 1

Dinal24
Dinal24

Reputation: 3192

You have a bug in this line:

 if (colliding = false) {

Which should be,

 if (colliding == false) // or if(!colliding)

Upvotes: 2

Related Questions