- Home /
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?
Does your character have a collider on it or a character controller on it?
Answer by iHaveReturnd · Jul 16, 2012 at 01:01 AM
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.
I tried the same thing, and the floor still didn't change.
Answer by Avaista · Jul 16, 2012 at 04:05 AM
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. ;)
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.
I am very sorry to inform you that none of the above solutions worked, even in the slightest. Thank you for the very informative answer though!
That is very interesting.
So... The cube on the floor has a collider, that is not a trigger, that has a rigidbody, without using gravity.
The player has a character controller, with a collider that extends farther out than his controller capsule plus his skinWidth?
This does not work with kinematic on the rigid body set to either on or off?
If this is not working for you, just make a [damn] trigger and add it above the cube.
I did this too. Nothing is working and I don't know why.
Okay.......I have a random question...Go To Edit->ProjectSettings->Time
Is the timescale 0? if so turn it to 1.
If this does not work, we can set it up for you to send me the project(if your are allowed to do so) and I can take a more indepth look at the scene, if you want.
It was already 1. So lets set up me sending you the project, as I am at wits end with it.