Objects (has a rigidbody), acts very weird when collider added
HI guys , I'm new to Unity and I've been building a sort of paper toss like game where you swipe objects into a bin, when I have no collider the objects are thrown fine and all works well. But when I add a collider to the objects so that the score system can detect when the object goes in , the objects start going crazy and spin out. The objects all have a rigidbody. Just wondering if anyone could help me be able to have the collider on the object without it going crazy. Thanks heaps !!!!!!!!
this the swiping script that I'm using : using System.Collections; using System.Collections.Generic; using UnityEngine; using Vuforia;
public class SwipingScript : MonoBehaviour {
Vector2 startPos, endPos, direction; // touch start position, touch end position, swipe direction
float touchTimeStart, touchTimeFinish, timeInterval; // to calculate swipe time to sontrol throw force in Z direction
[SerializeField]
float throwForceInXandY = 1f; // to control throw force in X and Y directions
[SerializeField]
float throwForceInZ = 50f; // to control throw force in Z direction
[SerializeField] private GameObject[] throwableObjects; //throwable object prefabs
[SerializeField] private GameObject hands;
GameObject currentThrowable; //object we're currently throwing
Rigidbody rb;
void Start() { EquipNewThrowable(); rb = currentThrowable.GetComponent(); }
// Update is called once per frame void Update () { GetTouchStart(); GetTouchRelease(); }
void GetTouchStart() { // if you touch the screen if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { Debug.Log("Got in if!"); // getting touch position and marking time when you touch the screen touchTimeStart = Time.time; startPos = Input.GetTouch (0).position; } }
void GetTouchRelease() { // if you release your finger if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended) {
// marking time when you release it
touchTimeFinish = Time.time;
// calculate swipe time interval
timeInterval = touchTimeFinish - touchTimeStart;
// getting release finger position
endPos = Input.GetTouch (0).position;
// calculating swipe direction in 2D space
direction = startPos - endPos;
ThrowObject(direction);
}
}
void ThrowObject(Vector3 direction) { // add force to balls rigidbody in 3D space depending on swipe time, direction and throw forces
rb = currentThrowable.GetComponent<Rigidbody>();
rb.isKinematic = false;
rb.AddForce (- direction.x * throwForceInXandY, - direction.y * throwForceInXandY, throwForceInZ / timeInterval);
EquipNewThrowable();
}
//after each throw
void EquipNewThrowable()
{
int randomizer = Random.Range(0, throwableObjects.Length);
currentThrowable = Instantiate(throwableObjects[randomizer]);
currentThrowable.transform.position = hands.transform.position;
rb = currentThrowable.GetComponent<Rigidbody>();
currentThrowable.SetActive(true);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612210934im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody randomly going through collider 1 Answer
Rigidbody wont move after changing collider.bounds.center 0 Answers
Character doesn't collide with the floor. 0 Answers
Error NullReferenceExceptionwhot isk this?? 0 Answers
Rigidbody moving on platforms 0 Answers