0
\$\begingroup\$

I am working on a hyper-casual game project which is very similar to Matching Cubes. Initially, I was using transform to stack blocks under my cylinder(player object). But in the game there will be a ramp to jump through it and by using transform I was ignoring the physics and it passes through the ramp. So I changed it a little bit by utilizing the rigidbody for the cylinder. If there is no block it jumps through the ramp but I need it to jump with blocks. The problem is I couldn't find a way to stack them under the cylinder by using rigidbody. Tried MovePosition or AddForce but it does not work at all.

How can I stack the blocks under the cylinder but also make them jump through the ramp together?

Here is my StackManager.cs . There is a Regulator function that will check the 'picks' list and regulate the positions. It is the function where I handle all positioning.

I also tried making the positioning by transform and when it collides with ramp, stop the Regulator() and AddForce(Vector3.up*offset) but it did not move up a bit.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;

public class StackManager : MonoBehaviour
{
    public static StackManager instance;
    [SerializeField] private float distanceBetweenObjs;
    [SerializeField] private Transform prevObject = null;
    [SerializeField] private Transform parent;
    [SerializeField] private Transform cylinder;
    [SerializeField] private Transform trailer;
    private List<Transform> picks; // Use this to order gate and random gate
    private Vector3 firstPosition;
    private int comboCounter; // fever mode tracker
    private Rigidbody rb;
    private Transform prev;

    private void Awake() {
        if(instance == null) {
            instance = this;
        }
    }

    void Start()
    {
        rb = cylinder.gameObject.GetComponent<PlayerController>().rb;
        comboCounter = 0;
        picks = new List<Transform>();
        firstPosition = new Vector3(cylinder.position.x, cylinder.position.y, cylinder.position.z);
    }

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


    void CheckChildren() {
        List<Transform> children = new List<Transform>();
        foreach (Transform child in picks)
        {
            children.Add(child);
        }
        
        for(int i = 0; i < children.Count - 2; i++) {
            if (children[i].isSameMaterial2(children[i+1], children[i+2])) {
                comboCounter++;
                Destroy(children[i].gameObject);
                Destroy(children[i+1].gameObject);
                Destroy(children[i+2].gameObject);
                picks.Remove(children[i]);
                picks.Remove(children[i+1]);
                picks.Remove(children[i+2]);
            };
        }
        if(comboCounter == 3) {
            SpeedBoost(); //fever mode
            comboCounter = 0;
        }
    }

    public void PickUp(GameObject pickedObj){
        pickedObj.tag = "Picked";
        pickedObj.transform.parent = parent;
        picks.Add(pickedObj.transform);
    }

    private void Regulator(){
        //Position of cylinder
        //set y value based on the # of children objects
        /**
        *hold the first position of cylinder
        *make calculation by referencing it
        *reference + localScale.y + 0.1f:
        */
        Vector3 newPos = new Vector3(cylinder.position.x, firstPosition.y, cylinder.position.z);
        foreach (Transform child in picks)
        {
            newPos.y += child.localScale.y + 0.1f;
        }
        //cylinder.position = newPos;
        rb.MovePosition(newPos);
        //Position of children
        if(picks.Count>0) {
            prevObject = picks[picks.Count-1];
        }
        /**
        *For each child
        *   cylinder-0.1f-pick-0.1f-pick-...
        */
        prev = cylinder;
        for(int i = 0; i < picks.Count; i++)
        {
            if(i==0){
                picks[i].position = new Vector3(prev.position.x, prev.position.y-1.2f, prev.position.z); 
                //picks[i].gameObject.GetComponent<Rigidbody>().position(new Vector3(prev.position.x, prev.position.y-1.2f, prev.position.z));
            } else {
                //picks[i].gameObject.GetComponent<Rigidbody>().MovePosition(new Vector3(prev.position.x, prev.position.y-prev.localScale.y -0.1f, prev.position.z));
                picks[i].position = new Vector3(prev.position.x, prev.position.y-prev.localScale.y -0.1f, prev.position.z); 
            }
            prev = picks[i];
        }
        //Position of trailer object
        if(picks.Count>0) {
            trailer.position = new Vector3(prev.position.x, prev.position.y-0.2f, prev.position.z);             //relocate the trailer object under last pick
            trailer.GetComponent<TrailRenderer>().material = prev.GetComponent<MeshRenderer>().material;        //change the color of the trail
        }
        CheckChildren();        //check 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.Sort((x, y) => string.Compare(x.GetComponent<MeshRenderer>().sharedMaterial.name, y.GetComponent<MeshRenderer>().sharedMaterial.name));
        
    }
    

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

    async public void onObstacle(Transform pickToDestroy) {
        pickToDestroy.gameObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition; 
        pickToDestroy.parent = null;
        await Task.Delay(200);
        picks.Remove(pickToDestroy);
    }

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


```
\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .