Hi guys. I have a gameObject called Red (just a cube) that will reduce the size of my Player gameObject upon collision. The script for that is:
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player")) {
transform.localScale += new Vector3(-.05F, -.05f, -.05f);
Destroy(gameObject);
}
}
However, this has no limit and the Player decreases in size endlessly whenever it hits a new Red gameObject (Red is an obstacle that spawns every 2 seconds and the Player must dodge it) and is no longer visible on the screen.
How could I adapt this script so that the scale of the Player has a minimum limit so that it’s always visible, even if tiny? e.g. 0.1 scale?
Thanks a lot for your quick reply and helping me. I’m a noob however and unsure where I should implement this - should it go on my Player script or Red script (where the Collision if statement is)?
Player Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Vector2 targetPos;
public float Yincrement;
public float speed;
public float maxHeight;
public float minHeight;
public Vector3 min = new Vector3(0.1f, 0.1f, 0.1f);
float s = transform.localScale.x
{
s += -.5f;
s = Mathf.Clamp(s, 0.1f, 1f); // clamp the scale
transform.localScale = new Vector3(s, s, s);
}
private void Update() {
transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.y < maxHeight) {
targetPos = new Vector2(transform.position.x, transform.position.y + Yincrement);
} else if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.y > minHeight) {
targetPos = new Vector2(transform.position.x, transform.position.y - Yincrement);
}
}
}
Red script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Red : MonoBehaviour
{
public float speed;
private void Update()
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player")) {
transform.localScale += new Vector3(-.05F, -.05f, -.05f);
}
}
}
If I understand right, when the box (red) hits the player, you want the player to get smaller. So in OnTriggerEnter2D of the Red script, you check if other is the “Player”. But what follows next seems like a mistake: you modify Red’s transform to decrease the box’s scale. If the player is supposed to get smaller, then you want to get the transform of variable other and perform the clamping-scale-reduction™ there so it affects the player.
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player")) {
var tr = other.transform; // player transform
float s = tr.localScale.x;
s += -.5f; // decrease scale
s = Mathf.Clamp(s, 0.1f, 1f); // clamp the scale
tr.localScale = new Vector3(s, s, s);
}
}
I’ve now updated my Red script to the following (i.e. swapped out the part of the script under OnTriggerEnter2D with your version) but the Player still disappears.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Red : MonoBehaviour
{
public float speed;
private void Update()
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player")) {
var tr = other.transform; // player transform
float s = tr.localScale.x;
s += -.5f; // decrease scale
s = Mathf.Clamp(s, 0.1f, 1f); // clamp the scale
tr.localScale = new Vector3(s, s, s);
}
}
}
I can see that the -5.f // decrease scale is working because each time Player hits Red, it halves in size, but the clamp doesn’t seem to prevent the Player disappearing altogether. What do you think I’ve done wrong/missed?
oid OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player")) {
var tr = other.transform; // player transform
float s = tr.localScale.x;
s += -.5f; // decrease scale
s = Mathf.Clamp(s, 0.5f, 1f); // clamp the scale
tr.localScale = new Vector3(1f, 1f, 1f);
}
}
With this, the first time Player is hit by Red, it reduces in half. Further hits by Red do not continue to decrease its size - this is great! But, I want this to be gradual e.g. Player stops decreasing in size when it becomes half its original size, such as decreasing by 10% each time Red hits it. I’ve changing the number for // decrease scale but this does not seem to have any effect. Any ideas why?
That’s a bit confusing b/c if you have tr.localScale = new Vector3(1f, 1f, 1f);
then you shouldn’t have any changes to scale going on, which makes me suspect you may be modifying your object elsewhere. I’d look for other places where you’re changing the scale of the player.
Thanks for your help Lo-renzo. I’ve had some success changing this but not as much as I’d like. But, I have learned something useful - having this mechanic in my game is adding a level of complexity that I think will make the game unfriendly and too busy, so I have decided to drop it for MVP. Perhaps I will introduce it later. Cheers.