Changing the color of a block when a player walks on it?

I want to make a script that changes the color of a block when the player touches it. I am trying

public Transform myTransform;

void Start () { myTransform = transform; }

void OnCollisionEnter (){ myTransform.renderer.material.color = Color.green; }

This code however is only working when a simple object with a rigid body touches it. And adding a rigidbody, or changing the collider for the player doesn’t work. What will fix my script, and why doesn’t this one work?

I just tested this out in unity and found that the character is sitting on top of the ground. If I manually moved the player controller down into the ground in unity the ground would change green. So I believe the controller is having itself sit above it, causing the collision to not really happen.

I’m fairly new to unity so that may not be the case, but that’s what I found from trying.

Okey Dokey, Collisions seem to be the word of the day today.

Firstly, Please go to
http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
and scroll down to the bottom.

Welcome to the Collision Action Matrix. Learn it. Love it. Love It. LOVE IT.

Now you are working with a character controller, which is a bit different. It inherits from collider, has a capsule collider, but does not quite act like one.

Try making the cube on the floor a Kinematic rigid body.

If this does not work(I don’t think it will), add a capsule collider to the character controller.
Make sure that the radius of this collider is greater than the character controller’s radius + the skin width. Now give it a shot. The cube you are walking on should be a rigidbody, if it is not then I guess you didn’t love it enough. :wink:

The following is more informational, the above should have fixed the issue, let me know if it didn’t.

The skin width is an odd thing that the documentation does a sub par job(IMO) of explaining(at least when considering how often the component is used)

It allows objects to penetrate the Character Controller.

Now looking at the capsule, you would think: “oh: so it can go up to {skinWidth} into the capsule.”

Not so much. In reality, imagine it as a force field around the capsule with a thickness of {skinWidth}. If some thing enters that force field, it will not let the controller move in that direction, but also doesn’t force it out. This results in less jitter.

To see this, put the skin width really high and pull the capsule up and play(assuming the capsule has some gravity (either SimpleMove or gravity added Move) Watch as you float in the air like a levitating Multivitamin.

Also, another solution would have been to use OnControllerColliderHit(), which you can check out here.