So i’m makind a 2d platformer and as far as i can tell my script is good. there aren’t any errors but its not working. the major problems are that he isn’t being reacting to physics, and isn’t regestering collisions. meaning that he floats in and out of other objects and won’t trigger any of my pickups or the end level trigger. i have a character controler applied to the character mesh. i have gravity in the code, i have the script applied to the character. however, i have ridgedbodys falling from above and they react to him just like they should. i can’t figure it out. and i’ve been looking at it to long to figure it out. and yes, feel free to rub it in, i’m sure it will be some tiny but glaringly obvious thing that i’m doing wrong.
here is the code.
`var walkspeed: int = 5;
var jumpHeight: int = 10;
var isGrounded: boolean = false;
var gravity : float = 9.81;
function Update () {
//Makes Player fall if in the air
if (isGrounded == false){
Physics.gravity = Vector3 (0,-gravity,0);
}
//Move Right
if (Input.GetKey (KeyCode.D)){
transform.Translate (0,0, walkspeed*(Time.deltaTime));
transform.rotation = Quaternion.Euler(0,90,0);//Always looking right
}
//Move Left
if (Input.GetKey (KeyCode.A)){
transform.Translate (0,0, walkspeed*(Time.deltaTime));
transform.rotation = Quaternion.Euler(0,-90,0);//Always looking left
}
//calls Jump function
if (isGrounded){
if (Input.GetKey (KeyCode.Space)){
Jump();
}
}
}
function Jump(){
Physics.gravity = Vector3 (0,gravity,0);
transform.Translate (0,jumpHeight*(Time.deltaTime),0);
yield WaitForSeconds(.25);
isGrounded=false;
}
function OnControllerColliderHit (hit:ControllerColliderHit){
//Picks up Money
if(hit.gameObject.tag =="Money")
{
Destroy(gameObject);
GameManager.Money ++;
print("Money: "+ GameManager.Money);
}
//Respawn after falling
if (hit.gameObject.tag =="DeathBox")
{
transform.position = Vector3(-3,5,0);
}
//Trigger to end level after player has picked up 'Pizza' and 'Soda'
if (hit.gameObject.tag =="Finish")
{
if(hit.gameObject.CompareTag ("Player") && GameManager.Pizza == true && GameManager.Soda == true)
{
print("You Win!");
}
}
//Changes players state to grounded
if (hit.gameObject.tag =="Ground")
{
isGrounded=true;
}
}`