- Home /
How to test colliders C#
I have a rotating ball on a plane that the player rotates. I need to test/check to see that the ball is still on the plane (that way, if not, I can stop the game).
I've tried using raycast with no success, and that seems to be because both objects have the ability to rotate. Here's what I've used:
Vector3 Up = transform.TransformDirection(Vector3.up);
if (Physics.Raycast(transform.position, Up, 10))
print("There is something in front of the object!");
It seems I need to not only check if the two objects are touching, but I need to check a little radius around the ball since it can end up slightly off the plane if you tilt the plane to it's extremes. I thought raycast would be perfect here, but no avail. Thanks for the help.
Answer by DJSwiti · Apr 16, 2012 at 10:13 PM
public void OnCollisionStay(Collider col)
{
if (col.gameObject.tag == "wall")
Debug.Log("There is something in front of the object!");
}
with that script attached to the plane and the sphere tagged as a wall or vice versa.
It's saying "OnCollisionStay" is a script error. Is it C#?
Ok, "OnCollisionStay" gives me no errors (looks like your original answer said "Collider col" when it needed "Collision col", at least according to the link there.
Problem is I put that code in the 'plane' script (replaced "wall" with "ball") and I get no errors but I also get no debug message either.
Yea sorry it was Collision and not Collider.. But it means that your objects aren't in collision.. Have you tried to move the cube while in collision with the plane ? Do you still have no Debug.Log ?
did you tag the object or name the object? $$anonymous$$ake sure you use the proper one.
Answer by Dead Lincs · Feb 03, 2014 at 02:19 PM
I did it like this: (By the way my character controller etc are all written in C#. I can give you the package if you would like, just send me a message
using UnityEngine;
using System.Collections;
public class CrouchC : MonoBehaviour {
public float walkSpeed = 7.0F;
public float crouchSpeed = 3.0F;
public float runSpeed = 20.0F;
private CharacterMotorC chMotor;
private CharacterController ch;
private Transform tr;
private float dist;
private bool release = false;
private float vScale = 1.0F;
private float speed;
void Start(){
chMotor = GetComponent<CharacterMotorC>();
tr = transform;
ch = GetComponent <CharacterController>();
dist = ch.height / 2;
speed = walkSpeed;
}
void Update(){
InputCheckingRule ();
ApplyChanges ();
}
public void InputCheckingRule(){
if (release == false) {
vScale = 1.0F;
speed = walkSpeed;
}
if (chMotor.grounded && Input.GetKey ("left shift") || Input.GetKey ("right shift")) {
speed = runSpeed;
}
if (Input.GetKey ("right shift")) {
vScale = 0.35F;
speed = crouchSpeed;
release = true;
}
if (Input.GetKeyUp ("right shift")) {
release = true;
}
if (release == true) {
ReleaseCheckingRule();
}
}
public void ReleaseCheckingRule(){
Vector3 Up = transform.TransformDirection(Vector3.up);
if (Physics.Raycast (transform.position, Up, 10)) {
release = true;
} else {
release = false;
}
}
public void ApplyChanges(){
chMotor.movement.maxForwardSpeed = speed;
float ultScale = tr.localScale.y;
Vector3 temp = tr.localScale;
temp.y = Mathf.Lerp (tr.localScale.y, vScale, 5 * Time.deltaTime);
tr.localScale = temp;
Vector3 temp2 = tr.position;
temp2.y += dist * (tr.localScale.y - ultScale);;
tr.position = temp2;
}
}
Your answer
Follow this Question
Related Questions
How do I use a sphere collider to tell if my player is on the ground? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers