- Home /
Using RayCasting to check for floor and ceiling?
So I know a rigidbody or character controller would be easier for what im trying to do, but this is a learning exercise, so I wanna re-invent the wheel a little bit.
so im trying to a make a 2.5d playformer (animated sprite plane for actors, models for enviroment) and im working on my character controller, so far ive gotten ground detection working okay, as well as jumping, but im having issues lerping his velocity from a posative to a negative. heres my code.
 #pragma strict
 
 //the object that gets hit
 var hitObj : GameObject;
 //if on the ground or not
 var onGround : boolean = false;
 //bottom position
 var bottom : Transform;
 //jumping
 var jumping : boolean = true;
 //velocity
 var velocity : float = 0.0;
 //max fall velocity
 var maxFall : float = -0.5;
 
 function Start () 
 {
 
 }
 
 function Update () 
 {
     //check for input
     if(Input.GetKey("space") && jumping==false)
     {
         //jump
         jumping=true;
         transform.position.y+=0.1;
         velocity=.1;
     }
     else
     {
         //dont jump
     }
     //do jumping
     if(jumping)
     {
         transform.position.y+=velocity;
         velocity=Mathf.Lerp(velocity,maxFall,0.1);
     }
     //raycast for ceiling
     var hitU : RaycastHit;
     if(Physics.Raycast(transform.position,Vector3.up,hitU,1))
     {
         Debug.DrawRay(transform.position,Vector3.up,Color.cyan,1);
         if(hitU.transform.gameObject.tag=="Ground")
         {            
             velocity=-.1;
             transform.position.y-=0.2;
         }
     }
     //raycast for ground
     var hit: RaycastHit;    
     if(Physics.Raycast(transform.position,Vector3.down,hit,1.1))
     {
         Debug.DrawRay(transform.position,Vector3.down,Color.cyan,1.1);
         if(hit.transform.gameObject.tag=="Ground")
         {
             hitObj=hit.transform.gameObject;
             onGround=true;
             jumping=false;
             velocity=0;
         }
         else
         {
             onGround=false;
             hitObj=null;
         }
     }
     else
     {
         onGround=false;
         hitObj=null;
     }
 }
Now the issue is that when i hit the ceiling he just kind bounces between a posative and negative velocity and gets stuck there, im sure alot of this could be done better, but again just a learner project so im trying to figure it out as i go...
he gets stuck right about here 
Your answer
 
 
             Follow this Question
Related Questions
How to determine if the player can jump, without using raycasts. (2D) 1 Answer
issues with jumping and linecast2d and raycast2d 0 Answers
2D Slope acceleration 1 Answer
Network Client does not Groundcheck 0 Answers
Platfomer 2d game block destroying raycasting nullreference exception problem 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                