RayCast detected only after penetrating the body
So now i have a Sphere which is the player and a lot of tiles.
This sphere which is the player has a rigidbody only for the purpose of collider detection because i get wierd physics stuff happening to the sphere when isKinematic is false (If someone can help me with that problem pls check out my other question. Thanks in advance)
So the isKinematic is set to true
But as isKinematic is enabled, the ball move downwards (ie. gravity) so i had to write a script for gravity on my own
I got the gravity working but the ball doesnt stop when it hits a collider below it so i had to find if the ball is grounded using a function which uses raycasts to checkif theres an object below the sphere
Now the problem is the sphere keeps moving down and only stops when the sphere penetrates a little through the tiles below it.
So heres the script i attached to the sphere :
using UnityEngine;
using System.Collections;
public class PlayerManager : MonoBehaviour
{
// Variables
Collider collider;
public float ZSpeed;
public float XSpeed;
public float AccGravity;
Rigidbody rigidbody;
public float JumpForce;
public Vector3 Velocity;
public int GemsCollected = 0;
public float VerticalVelocity;
public int isGrounded;
public float RayDistance;
public float DistFromEnd;
public Vector3 Test1;
public float Test2;
//Variables
//Properties
public int IsGrounded ()
{
int r = 0;
if ( Physics.Raycast ( transform.position, Vector3.down, RayDistance ) )
{
r = 1;
}
else
{
r = 0;
}
return r;
}
//Properties
void Start ()
{
//rigidbody = this.gameObject.GetComponent<Rigidbody> ();
Collider collider = this.gameObject.GetComponent<Collider> ();
RayDistance = collider.bounds.extents.y + 0.001f;
}
void Update ()
{
if ((Test1 == new Vector3 (0f, 0f, 0f)) && (Test2 == 0) )
{
if ( IsGrounded () == 1 )
{
Test1 = transform.position;
Debug.Log ("Done");
}
}
Debug.DrawLine ( transform.position, transform.position - new Vector3 ( 0f, RayDistance, 0f ), Color.black );
{
RaycastHit a;
isGrounded = IsGrounded ( );
}
MovementController ();
Gravity ();
//ForwardMove ();
//VerticalMove ();
//Velocity = rigidbody.velocity;
}
public void MovementController ()
{
float XKey = Input.GetAxis ( "Horizontal" );
transform.position += new Vector3 ( XKey * XSpeed, 0f, ZSpeed ) * Time.deltaTime;
}
public void Gravity ()
{
if ( IsGrounded ( ) == 1 )
{
VerticalVelocity = 0;
if ( Input.GetKeyDown ( KeyCode.Space ) )
{
VerticalVelocity = JumpForce;
}
}
else
{
VerticalVelocity -= AccGravity * Time.deltaTime;
}
if ( VerticalVelocity != 0 )
{
transform.position += new Vector3 ( 0f, VerticalVelocity, 0f ) * Time.deltaTime;
}
}
/*
public void ForwardMove ()
{
//rigidbody.velocity = new Vector3 ( rigidbody.velocity.x, rigidbody.velocity.y, ZSpeed );
}
public void VerticalMove ()
{
float XKey = Input.GetAxis ( "Horizontal" );
if ( XKey != 0 )
{
rigidbody.velocity = new Vector3 ( XKey * XSpeed, rigidbody.velocity.y, rigidbody.velocity.z );
}
else
{
rigidbody.velocity = new Vector3 ( 0f, rigidbody.velocity.y, rigidbody.velocity.z );
}
}
*/
public void OnTriggerEnter ( Collider C )
{
if ( C.transform.tag == "Destroy" )
{
GameObject.Destroy ( this.gameObject );
Debug.Log ( "Destroy done" );
}
if ( C.transform.tag == "Jump" )
{
Debug.Log ( "Entered Collider" );
transform.position += new Vector3 ( 0f, JumpForce, 0f ) * Time.deltaTime;
Debug.Log ( "Jump Done" );
}
if ( C.transform.tag == "Gem" )
{
GameObject.Destroy ( C.gameObject );
GemsCollected += 1;
Debug.Log ( GemsCollected + " Gems Collected" );
}
}
}
At different heights from which the sphere is dropped the sphere penetrates a little more or less.
Before playing the scene :
After playing the scene (U need to zoom in to see the RayCast) :