- Home /
Player color change on collision
I have a CharacterController2D script in which I placed a public transform of playerColor1, 2, and 3. I made a prefab for each of these states and dragged them into my player in the editor window. I am just learning by the way and I am sort of just winging it here to see if I can do this. So that part is finished and here is that part of the controller script:
using UnityEngine;
using System.Collections;
public class CharacterController2D : MonoBehaviour {
private const float SkinWidth = .02f;
private const int TotalHorizontalRays = 8;
private const int TotalVerticalRays = 4;
private static readonly float SlopeLimitTangent = Mathf.Tan(75f * Mathf.Deg2Rad);
public Transform playerColor, playerColor2, playerColor3;
public LayerMask PlatformMask;
public ControllerParameters2D DefaultParameters;
public ControllerState2D State { get; private set;}
public Vector2 Velocity {get{return _velocity;}}
public bool CanJump{
Now, I want to make cubes throughout the level that will transform the player character into one of those given color states depending on the color of the cube the player collides with so I wrote the following script:
using UnityEngine;
using System.Collections;
public class ColorChange : MonoBehaviour {
public Transform myColor;
public CharacterController2D _controller;
void Awake() {
_controller = GetComponent<CharacterController2D> ();
}
public void OnCollisionEnter2D(Collision2D col){
CharacterController2D controller = col.gameObject.GetComponent<CharacterController2D>();
if (controller != null) { // collided with character controller
if (myColor != controller.playerColor)
controller.playerColor = myColor;
else{
if(myColor = controller.playerColor)
return;
}
}
}
}
I have no errors and I was pretty proud of myself because it seems that everything is all the way it should be, except that when I collide with said cube, nothing happens. So I must be missing something, probably a pretty big part. Do I need to instantiate the controller in its new color after collision? I really don't know what is missing here. The actual method of switching the character's transforms apparently.
I could really use the help because this is a game mechanic I am really interested in implemening in my platformer.
Thanks.
I have used the console and the player cube seems to know its color as does the color change cube. I tried instantiating the myColor and that does work sort of, but the new cube appears in the scene view and not the game view.
Answer by Jeff-Kesselman · Jun 20, 2014 at 08:25 PM
You have a good start here, but here is one basic error:
if(myColor = controller.playerColor)
is wrong, it should be
if(myColor == controller.playerColor)
Single equals is assignment, dual equals is comparsion.
Also, a piece of advice, dont use naked ifs (ifs not followed by { }). It will just confuse your code. Always use {} after ifs or loop constructs, even if the body is only one statement.
Trust an experienced programmer, if you don't, it WILL eventually bite you.
So, with that error fixed and your code cleaned up a biut it would look like:
using UnityEngine;
using System.Collections;
public class ColorChange : MonoBehaviour {
public Transform myColor;
public CharacterController2D _controller;
void Awake() {
_controller = GetComponent<CharacterController2D> ();
}
public void OnCollisionEnter2D(Collision2D col){
CharacterController2D controller =
col.gameObject.GetComponent<CharacterController2D>();
if (controller != null) { // collided with character controller
if (myColor != controller.playerColor){
controller.playerColor = myColor;
} else {
if(myColor == controller.playerColor) {
return;
}
}
}
}
}
Note that once you have it organized you can see that the second if is in fact redundant and can be removes, making your code:
using UnityEngine;
using System.Collections;
public class ColorChange : MonoBehaviour {
public Transform myColor;
public CharacterController2D _controller;
void Awake() {
_controller = GetComponent<CharacterController2D> ();
}
public void OnCollisionEnter2D(Collision2D col){
CharacterController2D controller =
col.gameObject.GetComponent<CharacterController2D>();
if (controller != null) { // collided with character controller
if (myColor != controller.playerColor){
controller.playerColor = myColor;
} else {
return;
}
}
}
}
Having done that, you might notice that your return is also redundant as, if it weren't there, it would just drop out the bottom and return anyway. So you can eliminate that too and further simplify your code:
using UnityEngine;
using System.Collections;
public class ColorChange : MonoBehaviour {
public Transform myColor;
public CharacterController2D _controller;
void Awake() {
_controller = GetComponent<CharacterController2D> ();
}
public void OnCollisionEnter2D(Collision2D col){
CharacterController2D controller =
col.gameObject.GetComponent<CharacterController2D>();
if (controller != null) { // collided with character controller
if (myColor != controller.playerColor){
controller.playerColor = myColor;
}
}
}
}
Once you've done that, if it still isnt happening, you need to see if the collision routine is being called at all, so put a Debug.Log at the top:
using UnityEngine;
using System.Collections;
public class ColorChange : MonoBehaviour {
public Transform myColor;
public CharacterController2D _controller;
void Awake() {
_controller = GetComponent<CharacterController2D> ();
}
public void OnCollisionEnter2D(Collision2D col){
Debug.Log("Collided with "+col.gameObject.name);
CharacterController2D controller =
col.gameObject.GetComponent<CharacterController2D>();
if (controller != null) { // collided with character controller
if (myColor != controller.playerColor){
controller.playerColor = myColor;
}
}
}
}
Let us know the result of that much.
That didn't really seem to do anything. I should mention perhaps, in case I haven't, that I made a prefab of each character's colors and dropped the prefabs into the public transform locations.
I also set the box collider as a trigger which I believe I should do? Either way, the player cube just goes right through the object with no results at all. What I thought would happen is that if I set the color change object to purple, that it would change the player cube to its purple prefab.
Okay, so I do have it colliding with the object I wish to have initialize the color change. I used a trigger ins$$anonymous$$d that way I can have an on trigger exit to make it disappear and I have it reappearing after I die. However, it is not actually changing the color of my player :(
Your answer
Follow this Question
Related Questions
Particle System with multple collision behaviors for different layers? 0 Answers
Are Character Controllers inefficient? 1 Answer
2D - Detect Slopes x Walls 1 Answer
Player pass each other laterally but can step on each other 0 Answers
Pushing against Edge Collider wall stops player from falling 0 Answers