Stopping objects from appearing within a collider

Hello everyone, I’m currently making a game where you play as a Rock rolling around slopes and whatnot, and the primary mechanic is the ability to switch to pebble and boulder form at any time. Currently, I have it so that switching to the other two forms keeps the same momentum and speed, which is exactly what I want. Unfortunately, this also means that when boulder form is switched to, as his sprite and hitbox are much larger, he clips into the ground extremely easily. Is there a way I could make it so that when he’s switched to, he won’t appear in the ground and become stuck?

Here’s my code, which is C#:

using UnityEngine;
using System.Collections;

public class Igny : MonoBehaviour {

    //Movement Stuff
    public float speed = 1f;
    public Vector2 velocity;

    // Transfer stuff
    public Vector3 loc;

    // Responsible for states
    public enum FormStates {Pebble, Rock, Boulder, Dead, Victorious}
    public FormStates iState = new FormStates();

    // Holders for game objects
    public GameObject Pebble;
    public GameObject Rock;
    public GameObject Boulder;
    public GameObject Water;

    // Defaulting which forms are active
    private static Igny instance = null;
    public static Igny Instance {get {return instance; }}

    //Dead bool
    public bool Dead = false;

    // On awake
    void Awake () {

        iState = FormStates.Rock;
        instance = this;
        Pebble.SetActive (false);
        Rock.SetActive (true);
        Boulder.SetActive (false);
        Water.SetActive (true);

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

        if (Input.GetKeyDown (KeyCode.R)) {

            if (GUIManager.Instance.isPaused == true) return;
            Application.LoadLevel(0);
            //Change this so that the main menu is skipped, the player is just placed back and the level itself is reverted

        }

        if (iState == FormStates.Pebble) {

            loc = Pebble.transform.position;
            Water.SetActive (true);

        }

        if (iState == FormStates.Rock) {
           
            loc = Rock.transform.position;
            Water.SetActive (false);

        }

        if (iState == FormStates.Boulder) {
           
            loc = Boulder.transform.position;

            Water.SetActive (false);

        }

        if (Input.GetKeyDown (KeyCode.Space) && !Dead) {
           
            GUIManager.Instance.OnPausePress();
           
        }
       
        if (iState == FormStates.Dead) {
           
            Dead = true;
            Pebble.SetActive (false);
            Rock.SetActive (false);
            Boulder.SetActive (false);
            //Instantiate death stuff here
           
        }

        if (GUIManager.Instance.isPaused == true) return;

        if (Input.GetKeyDown (KeyCode.Q) && !Dead) {

            if (iState == FormStates.Pebble) return;

            iState = FormStates.Pebble;

            Pebble.SetActive (true);
            Rock.SetActive (false);
            Boulder.SetActive (false);
            Pebble.transform.position = loc;


                }

        if (Input.GetKeyDown (KeyCode.W) && !Dead) {

            if (iState == FormStates.Rock) return;

            iState = FormStates.Rock;

            Pebble.SetActive (false);
            Rock.SetActive (true);
            Boulder.SetActive (false);
            Rock.transform.position = loc; 
           
        }

        if (Input.GetKeyDown (KeyCode.E) && !Dead) {

            if (iState == FormStates.Boulder) return;

            iState = FormStates.Boulder;

            Pebble.SetActive (false);
            Rock.SetActive (false);
            Boulder.SetActive (true);
            Boulder.transform.position = loc; 
           
        }

    }

    // Physics updates
    void FixedUpdate () {
        //NEW STUFF
        if (iState == FormStates.Rock) {

            if (GUIManager.Instance.isPaused == true) return;

            rigidbody2D.velocity += velocity;
            float h = Input.GetAxis ("Horizontal");
            velocity = new Vector2 (h * speed, 0);

        }
    }
}

Also, bonus points to anyone who could tell me how to stop the boulder and pebble from spinning like the rock when they’re swapped to. I’m assuming I would just have to freeze it’s rotation when the form is swapped however I’m quite green to coding and haven’t really been able to figure out a way to type this out.

Any help would be much appreciated, thank you.

Just figured I’d bump this to see if anyone could think of anything! Any suggestions would be much appreciated

You could play an animation which throws the ball into the air when it morphs? Kind of a cheat but it’s 1 of those things which could make it look better but also save coding time.

I am a bit green to coding in Unity, myself, but I think I might be able to help you out. When you switch between different forms, maybe you could check the y coordinate of the ground you’re currently on, and adjust the loc variable accordingly.

For example, when you change from the rock form to the boulder, check loc.y, and then adjust loc.y by adding the radius of the boulder. That way, it will be shifted up when you switch forms.

Give that a try? :slight_smile:

That actually seems like it could work! I’ll try it out then let you know how it goes! Thanks guys