- Home /
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. :)
Answer by sjefke31 · Feb 07, 2012 at 03:51 PM
Nevermind but thanks for the help. I fixed it using raycasting, works great now. :)
Answer by sjefke31 · Feb 07, 2012 at 01:01 PM
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!!
Answer by green_core · Feb 07, 2012 at 03:01 PM
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.
Your answer
Follow this Question
Related Questions
How to move BoxCollider with animation? 1 Answer
Internal collisions 1 Answer
How to move BoxCollider2D so it covers a line renderer? 0 Answers
Adding a box collider to an object in csharp script 3 Answers
Tipping a cube over 2 Answers