- Home /
Platform color to kill player if player does not match
Hi,
I am just starting out with Unity and C# and have actually been making pretty good progress, but a game mechanic I want to introduce is giving me problems. It is important as I wish to use it quite frequently throughout the game.
As I said I'm just learning, but thought I'd take a whack at the code:
using UnityEngine;
using System;
class PlatformColor : MonoBehaviour{
public bool isBlack;
public bool isRed;
public bool isBlue;
private CharacterController2D _controller;
private Player _player;
public GiveDamageToPlayer _givedamagetoplayer;
public bool IsDead { get; private set; }
void Awake(){
_controller = GetComponent<CharacterController2D> ();
_player = GetComponent<Player> ();
_givedamagetoplayer = GetComponent<GiveDamageToPlayer> ();
}
void Update(){
if (isRed = true && Player.isRed == false)
GiveDamageToPlayer;
if (isBlack = true && Player.isBlack ==false)
GiveDamageToPlayer;
if (isBlue = true && Player.isBlue == false)
GiveDamageToPlayer;
}
}
I think I have the idea of it? Let me tell you what I THINK this script is doing. My idea is that by making the colors public bools, I can change them in the editor window in order to tell Unity which color the platform is because I don't know how else Unity would know. I am then calling my Controller, Player, and Damage scripts in the Start function.
Where I just get lost is the next part. I feel like my update function maybe looks... kinda right? I want the platform to kill the player if his color doesn't match the platform color, but changing the player color will be another whole issue I'm sure.
I feel like I should be using transform somewhere? Transform would = the platform is my understanding.
Can someone please help me out or tell me if I am way way way off on what I am attempting.
In the future please format your code properly using the code b;ock (the button with 101 010 on it) or your post will be rejected. This first time I have done it for you -- moderator
You dont tell us how you get onto a platform? Do you jump onto it or otherwise collide with it?
If so then you want to use a Collider and an OnColliderEnter callback
Sorry, I do jump onto the platforms and I have a 2d box collider on both the player and the platforms.
okay, then in the collider just check the color field on the two objects for equality. Eg if this script was on the platform:
public void OnEnterCollision(Collision coll){
Player playerComponent = coll.gameObject.GetComponent<Player>();
if (playerComponent!=null) { // collided with a player object
if (myColor!=playerComponent.color){
Destroy(coll.gameObject);
}
}
}
Note that this is 3D code. I believe the callback for 2D collision and the 2D Collision object is similar but slightly different and have different names.
what is "myColor"? I am a little confuse. Is it the color of the platform? If so, can you show me the code you used to assign "myColor". Thank You
I think I understand that... So the code can actually be simpler thrn I had it? Do I even need bools or to call my other scripts?
Answer by Kossuranta · Dec 15, 2016 at 08:31 AM
This is how I would do it. Player will constantly raycast downwards and if it hits floor and player bool color isn't same as floor bool player will lose health on every tick. Damage is multiplied with Time.deltaTime which basically means that with damage 10 player will lose 10 hp in a second.
Numbers 1, 2 and 3 will change player color. This script would also contain player movement, but it's not related to question so I didn't code it here.
PlayerMovement.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float startingHealth = 100f;
public float health;
public float damage = 10f;
public LayerMask mask;
public Transform playerFeet;
public Color clrBlack;
public Color clrBlue;
public Color clrRed;
private bool isBlack;
private bool isBlue;
private bool isRed;
private Renderer rend;
private RaycastHit2D hit;
private Ray2D ray;
private void Awake()
{
rend = GetComponent<Renderer>();
}
void Start ()
{
health = startingHealth;
rend.material.color = clrRed;
isBlack = false;
isBlue = false;
isRed = true;
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
rend.material.color = clrBlack;
isBlack = true;
isBlue = false;
isRed = false;
}
else if(Input.GetKeyDown(KeyCode.Alpha2))
{
rend.material.color = clrBlue;
isBlack = false;
isBlue = true;
isRed = false;
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
rend.material.color = clrRed;
isBlack = false;
isBlue = false;
isRed = true;
}
hit = Physics2D.Raycast(playerFeet.position, -Vector2.up, 0.1f, mask);
Debug.DrawLine(playerFeet.position, playerFeet.position - Vector3.up * 0.1f);
if(hit)
{
if (!isBlack && hit.transform.gameObject.GetComponent<Tile>().isBlack)
TakeDamage(damage * Time.deltaTime);
else if (!isBlue && hit.transform.gameObject.GetComponent<Tile>().isBlue)
TakeDamage(damage * Time.deltaTime);
else if (!isRed && hit.transform.gameObject.GetComponent<Tile>().isRed)
TakeDamage(damage * Time.deltaTime);
}
}
void TakeDamage(float dmg)
{
health -= dmg;
}
}
Tile.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tile : MonoBehaviour
{
public bool isBlack;
public bool isBlue;
public bool isRed;
}
My test scene had 3 Tiles and Player with empty GameObject PlayerFeet as child. Starting health is the amount player has hp at the beginning and if you want to respawn player you can just health = startingHealth. Health is players current health that goes down when player takes damage and player would die when health
I have created new layer called Floor and all tiles are set to that layer and player has LayerMask variable called mask where Floor is set so our Raycast doesn't hit anything else than the floor tiles. PlayerFeet is new empty GameObject that is set to bottom on player object and will be used as source for the raycast to shoot downwards. And finally color that we use to recolor players material when number 1, 2 or 3 is pressed.
Floor tiles have Layer set to Floor and they have script Tile.cs where boolean is set manually and I have created three different materials for floors (Black/Blue/Red).
Oh damn, just saw it in front page because the new comment and was bored :/
Your answer
Follow this Question
Related Questions
Side scroller - rotate object towards player. 1 Answer
Animation on public transform from script 2D 0 Answers
How to alternate images in 2d unity 0 Answers