About 'Damage Affordance'!

In this video (objectives)…

  1. Create an array to hold our damage sprites.
  2. Change our sprite renderer depending upon how many hits a block has taken.

After watching (learning outcomes)… Change the sprite displayed for a block based upon how many hits it has taken.

(Unique Video Reference: 31_BR_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

Hey Rick,

Do you have a supplemental recommended reading list about game design in general?

When you used the word “affordance” like that it made me want to learn more about what else I don’t know.

Thanks!

1 Like

Hi there,

I really like The Art Of Game Design: A Book Of Lenses by Jesse Schelle. Its a great book that will really train your brain on how to think like a designer.

5 Likes

Very cool, I’ll pick it up.

Thanks for the info, Rick!
Looks like a good read.

Thanks again,
Chris

1 Like

Hi Rick!

I’ve been experimenting with creating different sizes of breakable blocks.

The damaged blocks sprites are 128x128. Some of my blocks are larger or smaller than 128x128. Whenever I damage a block, the damaged block sprite is always 128x128.

As a fix I’ve resized the damaged block sprites in gimp and added them to my sprites folder in my Block Breaker game. So I have multiple sprites that look identical, but are different sizes.

My question is, does Unity provide a more convenient way that allows a damaged block sprite retain the size of the block its assigned to? Or is the manual work around I’ve done the best way to keep the sprites consistent with one another?

Hi Vincent, have you tried changing the pixels per unit for your various sprites so that they look the same in game?

Ah yes! That is a much simpler way to get the sprites to be consistent with each other.

Thanks!

1 Like

Hey. In this lesson, I conducted several experiments, and I had a question. I have a prefab block, and I want to change the sprite of just one block in the click event.
But when I click on the mouse button, all the blocks change.
Could you help me?

Example part of code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Block : MonoBehaviour {

    [SerializeField] Sprite damageSprite;

	void Update () {
		if(Input.GetMouseButtonDown(0))
                {
                   GetComponent<SpriteRenderer>().sprite = damageSprite;
                }
        }
}

If you want this to happen on only 1 block and you know beforehand which block it is, then you could add your script to just that one instance of the block and not the prefab.

If however you want the player to be able to click on any block and change the characteristics of it then you would need to use OnMouseDown rather than GetMouseButtonDown and add a collider to the block prefab. GetMouseButtonDown will see if you push your button anywhere, whereas OnMouseDown is assessing if the buttons was pushed while over the top of the collider.

Thank you so much. Your answer help to me.
OnMouseDown solved my problem

1 Like

Awesome, good to hear!

Just wanted to throw out a suggestion for those that may be having trouble actually playing their game to see if their code updates worked correctly (I know there have been a couple times where we saw a Game Over screen on Rick’s videos before we actually saw what he was trying to show us): within your Play Space select your Lose Collider, expand the Box Collider 2D (if it needs expanding), and uncheck the Is Trigger checkbox. This will turn it into a regular wall that just bounces the ball back rather than making you lose.

This wasn’t a huge issue for me in the earlier lessons since I usually only had a couple of blocks and I was making sure to space them the exact distance away so the ball would bounce from one to the other easily finishing the level for me but as we’re adding more complexities that require us to “play” the game for a longer period of time to validate our changes this was an easy way to not get frustrated at myself (and my crappy mouse) for failing the level right before it did the thing I wanted to see. Just don’t forget to re-check the checkbox once you’re done or it’ll be impossible to lose your game.

Just a suggestion for anyone else that may have been having the same difficulties.

Rick,

I do have a suggestion for improving your code, which would eliminate any array out of bounds issue people might have had. You also won’t need to make maxHits viewable in the editor.

Simply set maxHits in the Start() method with this line of code:
maxHits = hitSprites.Length + 1; // The number of Sprites will cause this to be set

If no sprites are set, you get a value of 1. If 1 sprite is in the array, you get a value of 2 and so on.

This prevents the case where you set the maxHits to a number higher than the number of sprites in hitSprites, which can cause an array out of bounds error. By reducing the number of items that have to be set in the Unity Editor you reduce the chance for a mistake like that.

Regards,

Mike Riley (lvskiprof)

Privacy & Terms