I typed up a script (basically copying from here) which uses the OnCollisionStay function. I added some Debug notes so that I could see what state the script is in, whether it is detecting input or sensing collision and things like that. However, whenever OnCollisionStay is triggered, movement controls no longer register, and “Movement input received!” is no longer logged in the console. Is there something I’m doing wrong/not understanding here?
#pragma strict
public var speed : float = 5.0;
public var moveDirection : Vector3 = Vector3.zero;
public var jumpDirection : Vector3 = Vector3.zero;
public var jumpSpeed : float = 500.0;
public var isGrounded : boolean = false;
public var jumping : boolean = false;
public var inAir : boolean = false;
public var airControl : float = 0.5;
public var jumpClimax : boolean = false;
var contact : ContactPoint;
function Awake ()
{
}
function Movement ()
{
if(Input.GetAxis("Horizontal") || Input.GetAxis("Vertical"))
{
moveDirection = Vector3(Input.GetAxisRaw("Horizontal"), moveDirection.y, Input.GetAxisRaw("Vertical"));
this.transform.Translate((moveDirection.normalized * speed) * Time.deltaTime);
Debug.Log("Movement input received!");
}
if(Input.GetButtonDown("Jump") && isGrounded)
{
Debug.Log("Jump input received!");
jumping = true;
jumpDirection = moveDirection;
rigidbody.AddForce((transform.up) * jumpSpeed);
}
if (isGrounded)
this.transform.Translate((moveDirection.normalized * speed)*Time.deltaTime);
else if (jumping || inAir)
this.transform.Translate((jumpDirection * speed * airControl)*Time.deltaTime);
else if (inAir && rigidbody.velocity.y == 0.0)
{
jumpClimax = true;
}
}
function FixedUpdate ()
{
var capsuleCollider : CapsuleCollider = gameObject.GetComponent(CapsuleCollider);
if (!isGrounded)
{
if(Physics.Raycast(transform.position, -transform.up, capsuleCollider.height/2 + 0.2))
{
Debug.Log(capsuleCollider.height/2 + 0.2);
isGrounded = true;
jumping = false;
inAir = false;
jumpClimax = false;
}
else if (!inAir)
{
inAir = true;
jumpDirection = moveDirection;
}
Movement ();
Debug.Log("Execute movement!");
}
}
function OnCollisionExit (collisionInfo : Collision)
{
isGrounded = false;
}
function OnCollisionStay (collisionInfo : Collision)
{
Debug.Log("Collision staying!");
contact = collisionInfo.contacts[0];
if (inAir || jumping)
{
rigidbody.AddForceAtPosition(-rigidbody.velocity, contact.point);
Debug.Log("Applying jumping force!");
}
}