Hello! I’m programming some rails for balls to roll down. I made it so that the ball, as long as it is on both rails, will move along the forward vector of the rails until it is no longer on them. However, whenever it goes off the rails, it slowly glides to the ground instead of falling. I used a Rigidybody for physics.
Does anyone know why this would be?
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Belt : MonoBehaviour
{
private List<Rigidbody> CrossRefrence = new List<Rigidbody>();
public List<Rigidbody> rb = new List<Rigidbody>();
public GameObject OtherRail;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
for (int i = 0;i<CrossRefrence.Count;i++) {
if (OtherRail.GetComponent<Belt2>().rb.Contains(CrossRefrence[i])) {
rb.Add(CrossRefrence[i]);
CrossRefrence.Remove(CrossRefrence[i]); //This is was I needed to add
}
}
for (int i = 0;i<rb.Count; i++) {
Vector3 v = transform.forward*5;
v.y=0;
rb[i].velocity=v;
}
}
void OnCollisionEnter(Collision other)
{
CrossRefrence.Add(other.collider.gameObject.GetComponent<Rigidbody>());
}
void OnCollisionExit(Collision other)
{
if (rb.Contains(other.gameObject.GetComponent<Rigidbody>())) {
rb.Remove(other.gameObject.GetComponent<Rigidbody>());
}
}
}