Sunday, June 12, 2022
HomeGame Developmentunity - The right way to make the most of stacking blocks...

unity – The right way to make the most of stacking blocks below the participant object(cylinder) by utilizing rigidbodies in unity3d?


I’m engaged on a hypercasual recreation undertaking which is similar to Matching Cubes. Initially, I used to be utilizing rework to stack blocks below my cylinder(participant object). However within the recreation there shall be a ramp to leap by it and by utilizing rework I used to be ignoring the physics and it passes by the ramp. So I modified it a little bit bit by using the rigidbody for the cylinder. If there is no such thing as a block it jumps by the ramp however I would like it to leap with blocks. The issue is I could not discover a strategy to stack them below the cylinder by utilizing rigidbody. Tried MovePosition or AddForce but it surely doesn’t work in any respect.

How can I stack the blocks below the cylinder but in addition make them bounce by the ramp collectively?

Right here is my StackManager.cs . There’s a Regulator perform that can test the ‘picks’ record and regulate the positions. It’s the perform the place I deal with all positioning.

I additionally tried making the positioning by rework and when it collides with ramp, cease the Regulator() and AddForce(Vector3.up*offset) but it surely didn’t transfer up a bit.

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing System.Threading.Duties;

public class StackManager : MonoBehaviour
{
    public static StackManager occasion;
    [SerializeField] non-public float distanceBetweenObjs;
    [SerializeField] non-public Remodel prevObject = null;
    [SerializeField] non-public Remodel dad or mum;
    [SerializeField] non-public Remodel cylinder;
    [SerializeField] non-public Remodel trailer;
    non-public Listing<Remodel> picks; // Use this to order gate and random gate
    non-public Vector3 firstPosition;
    non-public int comboCounter; // fever mode tracker
    non-public Rigidbody rb;
    non-public Remodel prev;

    non-public void Awake() {
        if(occasion == null) {
            occasion = this;
        }
    }

    void Begin()
    {
        rb = cylinder.gameObject.GetComponent<PlayerController>().rb;
        comboCounter = 0;
        picks = new Listing<Remodel>();
        firstPosition = new Vector3(cylinder.place.x, cylinder.place.y, cylinder.place.z);
    }

    // Replace known as as soon as per body
    void Replace()
    {
        Regulator();
    }


    void CheckChildren() {
        Listing<Remodel> youngsters = new Listing<Remodel>();
        foreach (Remodel little one in picks)
        {
            youngsters.Add(little one);
        }
        
        for(int i = 0; i < youngsters.Rely - 2; i++) {
            if (youngsters[i].isSameMaterial2(youngsters[i+1], youngsters[i+2])) {
                comboCounter++;
                Destroy(youngsters[i].gameObject);
                Destroy(youngsters[i+1].gameObject);
                Destroy(youngsters[i+2].gameObject);
                picks.Take away(youngsters[i]);
                picks.Take away(youngsters[i+1]);
                picks.Take away(youngsters[i+2]);
            };
        }
        if(comboCounter == 3) {
            SpeedBoost(); //fever mode
            comboCounter = 0;
        }
    }

    public void PickUp(GameObject pickedObj){
        pickedObj.tag = "Picked";
        pickedObj.rework.dad or mum = dad or mum;
        picks.Add(pickedObj.rework);
    }

    non-public void Regulator(){
        //Place of cylinder
        //set y worth based mostly on the # of kids objects
        /**
        *maintain the primary place of cylinder
        *make calculation by referencing it
        *reference + localScale.y + 0.1f:
        */
        Vector3 newPos = new Vector3(cylinder.place.x, firstPosition.y, cylinder.place.z);
        foreach (Remodel little one in picks)
        {
            newPos.y += little one.localScale.y + 0.1f;
        }
        //cylinder.place = newPos;
        rb.MovePosition(newPos);
        //Place of kids
        if(picks.Rely>0) {
            prevObject = picks[picks.Count-1];
        }
        /**
        *For every little one
        *   cylinder-0.1f-pick-0.1f-pick-...
        */
        prev = cylinder;
        for(int i = 0; i < picks.Rely; i++)
        {
            if(i==0){
                picks[i].place = new Vector3(prev.place.x, prev.place.y-1.2f, prev.place.z); 
                //picks[i].gameObject.GetComponent<Rigidbody>().place(new Vector3(prev.place.x, prev.place.y-1.2f, prev.place.z));
            } else {
                //picks[i].gameObject.GetComponent<Rigidbody>().MovePosition(new Vector3(prev.place.x, prev.place.y-prev.localScale.y -0.1f, prev.place.z));
                picks[i].place = new Vector3(prev.place.x, prev.place.y-prev.localScale.y -0.1f, prev.place.z); 
            }
            prev = picks[i];
        }
        //Place of trailer object
        if(picks.Rely>0) {
            trailer.place = new Vector3(prev.place.x, prev.place.y-0.2f, prev.place.z);             //relocate the trailer object below final choose
            trailer.GetComponent<TrailRenderer>().materials = prev.GetComponent<MeshRenderer>().materials;        //change the colour of the path
        }
        CheckChildren();        //test for the 3-conjugate combo
    }

    public void ChangeRampState() {
        Debug.Log("prev.gameObject");
        Debug.Log(prev.gameObject);
        prev.gameObject.GetComponent<Collider>().isTrigger = false;
        rb.AddForce(Vector3.up * 300);
    }

    public void OrderPicks() {
            picks.Type((x, y) => string.Examine(x.GetComponent<MeshRenderer>().sharedMaterial.title, y.GetComponent<MeshRenderer>().sharedMaterial.title));
        
    }
    

    public void ShufflePicks() {
            picks = picks.Fisher_Yates_CardDeck_Shuffle();
        
    }

    async public void onObstacle(Remodel pickToDestroy) {
        pickToDestroy.gameObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition; 
        pickToDestroy.dad or mum = null;
        await Job.Delay(200);
        picks.Take away(pickToDestroy);
    }

    public void SpeedBoost() {
        cylinder.gameObject.GetComponent<PlayerController>().rb.velocity *= 2;
    }
    
}


```

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments