Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CrisisMode · Jun 20, 2014 at 03:06 AM · 2dplatformercolorsside-scrolling

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.

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeff-Kesselman · Jun 20, 2014 at 03:07 AM 0
Share

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

avatar image Jeff-Kesselman · Jun 20, 2014 at 03:09 AM 0
Share

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

avatar image CrisisMode · Jun 20, 2014 at 03:39 AM 0
Share

Sorry, I do jump onto the platforms and I have a 2d box collider on both the player and the platforms.

avatar image Jeff-Kesselman · Jun 20, 2014 at 03:44 AM 0
Share

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.

avatar image dasani2406 Jeff-Kesselman · Dec 15, 2016 at 05:46 AM 0
Share

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

avatar image aditya dasani2406 · Dec 15, 2016 at 08:26 AM 0
Share

Please let this thread live in peace now

avatar image CrisisMode · Jun 20, 2014 at 04:11 AM 0
Share

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?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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. Scene

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). Tile


tile.png (31.5 kB)
scene.png (129.2 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aditya · Dec 15, 2016 at 08:33 AM 0
Share

Brother this thread is 2 years old ...

avatar image Kossuranta aditya · Dec 15, 2016 at 08:34 AM 0
Share

Oh damn, just saw it in front page because the new comment and was bored :/

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

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

2D platformer - character clips into the ground 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges