Move Box Collider seperately from gameObject

Is there any way I can make a (for example) Box Collider attached to a gameObject move seperately from the gameObject itself? So when I press W the Box Collider moves, not the gameObject itself.

I tried the following, but it moves the whole gameObject including the Box Collider:

void Update()
{
    if (Input.GetKeyDown("w"))
    {
        this.gameObject.collider.transform.position = this.gameObject.collider.transform.position + new Vector3(0, 0, 0.32f);
    }
}

Anyone? Greatly appreciated. :slight_smile:

You can add a collider to a child object of your gameobject and then move this child object. It’s more comfortable, than moving a center of the collider.

Found a solution myself, but that results into other issues. With the following code I’m able to move the Box Collider:

void Update()
{
    BoxCollider boxCollider = GetComponent("BoxCollider") as BoxCollider;

    if (Input.GetKeyDown("w"))
    {
        boxCollider.center = boxCollider.center + new Vector3(2.56f, 0, 0);
    }
}

But then my next issue is, how do I let it detect for collision via OnTriggerEnter? As it doesn’t work anymore now, this is the code I used:

void OnTriggerEnter(Collider otherObject)
{
    if (otherObject.tag == "walls-blocks")
    {
        Debug.Log("(PlayerHitWall) Player hit: " + otherObject.name);
    }
}

Anyone? Help would be GREATLY appreciated!!

Nevermind but thanks for the help. I fixed it using raycasting, works great now. :slight_smile: