🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Need help shrinking/unshrinking my character

Started by
4 comments, last by LorenzoGatti 4 years ago

Hi! I'm attempting to create a mechanic where my character can shrink down to half scale to fit into smaller spaces on a key hold and return to normal scale when the key is released. I have tried to do this using both animation and local scale adjustments in the script. However, the problem I keep running into is that I can trigger the shrink using both ways but when I use the arrow keys, the character returns to normal scale. Is there something that's happening in update where the input manager is handling movement that is causing my sprite to constantly return to a normal scale state? If so, how do I maintain the shrunken size while moving until the key is released? I've also tried to do this with OnTriggerEnter, Stay and Exit, but the result is the same: the character will shrink, but the second I move it, it returns to normal size.

Please let me know if you need more information about my scripts or anything!

Advertisement

Looks more like you have a bug in your code.

It looks like you aren't processing multiple inputs (shrinking button and arrows instead of arrows alone) properly: when arrows are pressed you incorrectly behave as if the shrinking button isn't pressed, without checking or remembering its actual status.

There are significantly different typical ways to have this problem: if you don't figure it out on your own post code.

Omae Wa Mou Shindeiru

@LorenzoGatti So I ended up forgoing the button press and attempted to make the character grow and shrink through animation and trigger volumes. I understand what you're saying about it not storing the current status, that definitely seems to be what's happening. I tinkered with it a little bit, but I still don't understand exactly where the problem is occurring so I'm not sure where to implement a solution. I have a movement script for the character, as well as a script that attaches to the trigger volume. I'll also post a photo of the animator that's handling the shrinking and growing. The animations don't seem to be triggering properly either and it will go from “shrink" right into “grow” without waiting for the condition to be met.

I feel like there must be a much easier way to implement this mechanic.

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

public class ShadowMover : MonoBehaviour
{
    //movement variables
    [Range(0.0f, 10.0f)] public float moveSpeed;
    public float jumpForce;
    public float fallMultiplier = 3.5f;
    public float lowJumpMultiplier = 3f;

    Rigidbody2D _rb2d;
    Transform _transform;
    PolygonCollider2D _playerCollider;
    Animator _animator;

    float _horizontal;
    bool _isFacingRight;

    //skill variables
    SpriteRenderer _myRenderer;

    bool ignore;
    bool canPassThru;

    // Start is called before the first frame update
    void Awake()
    {
        _rb2d = GetComponent<Rigidbody2D>();
        _transform = GetComponent<Transform>();
        _playerCollider = GetComponent<PolygonCollider2D>();

        _animator = GetComponent<Animator>();
        _myRenderer = GetComponent<SpriteRenderer>();
        ignore = false;
    }

    // Update is called once per frame
    void Update()
    {
        MovePlayer();
        Jump();
    }

    private void LateUpdate()
    {
        FlipSprite();
    }

    private void MovePlayer()
    {
        float horizontal = Input.GetAxis("Horizontal");
        Vector2 moveVelocity = new Vector2(horizontal * moveSpeed, _rb2d.velocity.y);
        _rb2d.velocity = moveVelocity;
    }

    private void Jump()
    {

        if (Input.GetButtonDown("Jump") && _playerCollider.IsTouchingLayers(LayerMask.GetMask("Ground", "Shadow", "Protagonist")))
        {
            _rb2d.velocity = Vector2.up * jumpForce;
        }

        if (_rb2d.velocity.y < 0)
        {
            _rb2d.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
        }

        else if (_rb2d.velocity.y > 0 && !Input.GetButton("Jump"))
        {
            _rb2d.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;

        }
    }

    private void FlipSprite()
    {
        bool playerIsMovingHorizontally = Mathf.Abs(_rb2d.velocity.x) > Mathf.Epsilon;
        if (playerIsMovingHorizontally)
        {
            transform.localScale = new Vector2(Mathf.Sign(_rb2d.velocity.x), 1f);
        }
    }

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

public class ShrinkZone : MonoBehaviour
{
   Animator proAnim;

   void Start()
   {
       proAnim = GameObject.Find("Shadow").GetComponent<Animator>();
   }

   private void OnTriggerEnter2D(Collider2D otherCollider)
   {
       if (otherCollider.tag == "Shadow")
       {
           proAnim.SetTrigger("Shrink");
       }
   }

   private void OnTriggerExit2D(Collider2D otherCollider)
   {
       if (otherCollider.tag == "Shadow")
       {
           proAnim.SetTrigger("Grow");
       }
   }
}

Notably missing in this code:

  • The current size status (small, large, at a certain point of the shrinking or de-shrinking animation).
    I'd expect current size to affect things like _playerCollider (which should change size and possibly shape) or a transform that deforms it, movement velocities, etc.
  • The “Shrink” and “Grow” triggers: what happens when they are “set”?
  • The “conditions” you mention, which should prevent premature shrinking and de-shrinking.

How can you handle shrinking and de-shrinking with a fixed “shrink zone” on the map? Don't you actually need a button input?

Omae Wa Mou Shindeiru

This topic is closed to new replies.

Advertisement