stoping objects oncollision exit
how can i an exit script to this script so instead of stoping objects completely on exit it will like slowly come to a stop rather than instantly? public bool useFixedDirection; public Vector3 pullDirection; public float timescale = 1; public float pullSpeed; public float radius;
private SphereCollider _col;
private Vector3 _enterDirection;
private void Start()
{
_col = GetComponent<SphereCollider>();
}
private void Update()
{
_col.radius = radius;
Time.timeScale = timescale;
}
private void OnTriggerEnter(Collider other)
{
_enterDirection = (transform.position - other.transform.position).normalized;
}
private void OnTriggerStay(Collider other)
{
float _dist = Vector3.Distance(other.transform.position, transform.position);
float _ratio = _dist / radius;
if (useFixedDirection)
other.attachedRigidbody.AddForce(pullDirection * pullSpeed * _ratio);
else
other.attachedRigidbody.AddForce(_enterDirection * pullSpeed * _ratio);
}
private void OnTriggerExit(Collider other)
{
other.attachedRigidbody.velocity = Vector3.zero;
}
}
@Kilsnus
Answer by MarioSantoso · Apr 26, 2018 at 03:39 AM
Simply change the line in OnTriggerExit to
other.attachedRigidbody.drag = 1;
This will increase the drag once it exit the tornado causing it to slow down. And if you have gravity turned on, it will be pulled down also.
As you get more familiar with the codes, I believe you can further improve the simulation, like gradually increasing drag to create the effect you want, etc. Just play around and do experiments with the codes to create a believable simulation.
wow! works purfectly how i wanted it to! thanks again man! ya im still kindof a beguinner at scripting. i started unity like 3 months ago and right when i started i started on this tornado project and at first i had no idea what i was doing :D but ive figured it out for the most part i think, and have learned bits and pices of C# scripting along the way. in the summer im going to do a c# class so i can learn it and get better! thanks again man!
No problem, just shout out when you are stuck again. And good luck with your project and school.
Answer by himanshugupta159 · Jul 03, 2019 at 05:47 AM
Hi i have written a blog on -How colliders work in unity and how to interact with colliders using script. Pls visit and show some love. Thank you in advance.. link text