- Home /
Collisions with spherecast?
Hey, I am making a dodgeball game, I made the ball throwing system, but the ball (rigidbody) has high velocity, so it goes through objects (Collision detection is already in Continuous Dynamic), so I was wondering, maybe using a spherecast to detect collisions would do the trick, but I never used spherecast before, and I don't know how I could use it to detect collisions, expecially because I wanted to have a physics material with bounciness, some friction and that stuff, anyone can lead me to the right way?
Answer by robertbu · Aug 21, 2013 at 02:41 PM
For dealing with your ball going through things, go to Edit>Project Settings>Time and change the Fixed Timestep to 0.01 or perhaps even smaller.
Also take a look at this script:
http://wiki.unity3d.com/index.php?title=DontGoThroughThings
It uses Raycasting much as you were considering with your question.
with fixed Timestep everything moves slower, the ball is slower, the character is slower (I'm using rigidbody Controller), can I speed things up and it won't go through things or does it have to be slow like it is atm?
Edit: I tested speeding up the ball and it goes through things, anyway, the script you gave me is also not working, probably the ball is moving too fast
What specific 'rigidbody controller' are you using? What does your movement code look like? Typically changing the Fixed Timestep would not change the speed of movement.
It's the script from EteeskiTutorials, with some modifications
public class Player$$anonymous$$ovementScript : $$anonymous$$onoBehaviour {
public float $$anonymous$$ovement;
public float walkAcceleration = 1600f;
public float walkAccelAirRatio = 0f;
public float walkDeAcceleration = 0.3f;
[HideInInspector]
public float walkDeAccelerationVolx;
[HideInInspector]
public float walkDeAccelerationVolz;
public GameObject cameraObject;
public float maxWalkSpeed = 10f;
public Vector2 horizontal$$anonymous$$ovement;
public float jumpVelocity = 240f;
bool jumping = false;
static bool grounded = false;
public float maxSlope = 60f;
public AudioSource footSteps;
bool moving;
void Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
jumping = true;
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.S) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.A) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) {
if(grounded){
moving = true;
}
else{
moving = false;
}
}
else {
moving = false;
}
if (moving && !footSteps.isPlaying) {
footSteps.Play();
}
else if (!moving){
footSteps.Stop();
}
}
void FixedUpdate (){
horizontal$$anonymous$$ovement = new Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if(horizontal$$anonymous$$ovement.magnitude > maxWalkSpeed){
horizontal$$anonymous$$ovement = horizontal$$anonymous$$ovement.normalized;
horizontal$$anonymous$$ovement *= maxWalkSpeed;
}
rigidbody.velocity = new Vector3(horizontal$$anonymous$$ovement.x,rigidbody.velocity.y,horizontal$$anonymous$$ovement.y);
if(grounded){
float temp1x = $$anonymous$$athf.SmoothDamp(rigidbody.velocity.x, 0, ref walkDeAccelerationVolx, walkDeAcceleration);
float temp2z = $$anonymous$$athf.SmoothDamp(rigidbody.velocity.z, 0, ref walkDeAccelerationVolz, walkDeAcceleration);
rigidbody.velocity = new Vector3 (temp1x,rigidbody.velocity.y, temp2z);
}
transform.rotation = Quaternion.Euler(0f, cameraObject.GetComponent<$$anonymous$$ouseLookScript>().currentYRotation, 0f);
if (grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRatio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRatio* Time.deltaTime);
if (jumping && grounded)
rigidbody.AddRelativeForce(Vector3.up * jumpVelocity);
jumping = false;
}
void OnCollisionStay (Collision collision)
{
foreach (ContactPoint contact in collision.contacts){
if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
grounded = true;
}
}
void OnCollisionExit (){
grounded = false;
}
}
Your answer
Follow this Question
Related Questions
Ball jump on collision problem 1 Answer
Physics.SphereCast please help understand 0 Answers
Unrealistic bounce 2 Answers
How to move an object over a distance in a direction? 2 Answers