- Home /
Why this script not working?
I have this script:
public float threshold;
private bool isGrounded;
void OnTriggerEnter(Collision other)
{
foreach(ContactPoint temp in other.contacts) {
if(Vector3.Cross(temp.normal, Vector3.up) < threshold) {
isGrounded = true;
}
}
}
The error says that operatos of < cant be applied to Vector3 and float, how can I fix this?
What it does is check the collider bounds to see if it is grounded.
Answer by Radivarig · Apr 01, 2014 at 11:30 PM
The left side of your if statement is a Vector3, since that is what Vector3.Cross() returns.
The error occurs because you can't compare set of 3 floats with 1 float.
Vector that is returned there is also normalized which means its magnitude equals 1 so no point in using someNormalizedVector.magnitude since it is a fixed value.
My first thoughts for ground checking would be raycasting or using collider.isGrounded which natively does not give usable results but I made a workaround for that, you can find the code and unitypackage working scene with separated raycast scene, char controller and both combined with explanations here. Good luck!
Radivarig
Can you put it in a code please? I am new to raycasting.
I put this script but somehow it doesnt works when the character walks on slopes.
void IsGrounded() {
RaycastHit hitInfo;
if (Physics.Raycast(transform.position, Vector3.down, out hitInfo))
{
if (hitInfo.normal.y > InclinatedSlopes)
isgrounded = true;
}
else isgrounded = false;
}
I mean sorry this is the code I used, the one before doesnt work either.
void IsGrounded() {
if (Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f))
isgrounded = false; // Debe mantenerse en true
else isgrounded = true;
}