I need some help with my climb script !

Hey guys :slight_smile: how you all doing. well basically i just did this in 5 mins and i am facing a problem the player is able to climb but when i let go of the key the player start to fall down because gravity is being applied to him. How can i prevent this from happening. I am using grounded and if the player leaves the floor he starts to fall back down because the grounded function is no longer touching the floor please can you help me with this thank you

MCHALO

here is my script :

		public var MoveSpeed :float = 50;
		
					public var jumpSpeed : float = 55;
					
							public var MoveDirection = Vector3.zero;
							
										public var Gravity :float = 45;
										
												public var grounded : boolean = false;

var on: boolean;
var Player : GameObject;

  var localPosition : Vector3;
  
  var HeadBobOn: boolean = false;
  
  var headBobDistance : Vector3;
  
  var HeadBobSpeed : float = 3;
  
  var HeadBobTransform : Transform;
  
  var Movement : boolean = true;
  
  function Start (){

    Movement = true;
  //HeadBobTransform = transform;
  HeadBobOn = false;
  localPosition = HeadBobTransform.localPosition;


}


function Update () {


		if( grounded ){
		
               MoveDirection = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
               
               				MoveDirection = transform.TransformDirection(MoveDirection);
               				
               							MoveDirection *= MoveSpeed ;
 
                           HeadBobbing ();
                          
                          
                          
}

			if( grounded ) {
			
			if(Input.GetKey(KeyCode.Space)){
			
			
					MoveDirection.y = jumpSpeed;
			HeadBobOn = false;
			
			}
			
			//if(grounded == true){
			
		 //	HeadBobOn = true;
			
	//}
}

if(Input.GetKey(KeyCode.W) || (Input.GetKey(KeyCode.UpArrow))){

    HeadBobOn = true;
   

}

if(Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W)){




}
if(on == true){
  
 
if(Input.GetKey(KeyCode.C)){


   MoveDirection.y = MoveSpeed;


}
}

			MoveDirection.y -= Gravity * Time.deltaTime;
			
						var Controller = GetComponent(CharacterController);
						
							var Flags = Controller.Move( MoveDirection * Time.deltaTime);
							
									grounded = (Flags & CollisionFlags.CollidedBelow) !=0;
}



function HeadBobbing (){



	if(HeadBobOn == true){
		
		
		  var BobAmount = Mathf.Min(0.5, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));
		  
		var NexPositionX = localPosition.x + headBobDistance.x * Mathf.Sin(Time.time * HeadBobSpeed + Mathf.PI/2);
		  
		  HeadBobTransform.localPosition.x = (localPosition.x * (1 + BobAmount)) + ( NexPositionX *(BobAmount));
	     
  
		  
		  var NexPositionY = localPosition.y + headBobDistance.y * Mathf.Sin(Time.time * HeadBobSpeed + Mathf.PI/2);
		  
		  HeadBobTransform.localPosition.y = (localPosition.y * (1 - BobAmount)) + ( NexPositionY *(BobAmount));

}

}

function OnTriggerEnter(other : Collider) {
  
     on = true;
     
if(on == true){
if( other.gameObject.tag == "Enter Steps" ){
   // Destroy(other.gameObject);
   Debug.LogWarning("You are in the trigger and are ready to climb");

   
}
else{

on = false;


}
}
}

function OnTriggerExit(other : Collider) {
   
   on = false;
  if(on == false){
  Debug.LogWarning("You are out of the trigger");
if( other.gameObject.tag == "Enter Steps" ){
   // Destroy(other.gameObject);
  
   
  
   
}
else{

   on = true;
        
}
}
}

1 Answer

1

its ok i fixed it its a bit buggy but if you want the climb script then here you go :

		public var MoveSpeed :float = 50;
		
					public var jumpSpeed : float = 55;
					
							public var MoveDirection = Vector3.zero;
							
										public var Gravity :float = 45;
										
												public var grounded : boolean = false;

var on: boolean;

var ObjectWall : Transform;

var ClimbSpeed : float = 200;

   var NewDirections = Vector3.zero;

var Player : GameObject;

  var localPosition : Vector3;
  
  var HeadBobOn: boolean = false;
  
  var headBobDistance : Vector3;
  
  var HeadBobSpeed : float = 3;
  
  var HeadBobTransform : Transform;
  
  var Movement : boolean = true;
  
  function Start (){

    Movement = true;
  //HeadBobTransform = transform;
  HeadBobOn = false;
  localPosition = HeadBobTransform.localPosition;


}


function Update () {


		if( grounded ){
		
               MoveDirection = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
               
               				MoveDirection = transform.TransformDirection(MoveDirection);
               				
               							MoveDirection *= MoveSpeed ;
 
                           HeadBobbing ();
                          
                          
                          
}

			if( grounded ) {
			
			if(Input.GetKey(KeyCode.Space)){
			
			
					MoveDirection.y = jumpSpeed;
			HeadBobOn = false;
			
			}
			
			//if(grounded == true){
			
		 //	HeadBobOn = true;
			
	//}
}

if(Input.GetKey(KeyCode.W) || (Input.GetKey(KeyCode.UpArrow))){

    HeadBobOn = true;
   

}

if(Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W)){


    MoveSpeed = 400;
 

}

if ( on == true){


if(Input.GetKey(KeyCode.UpArrow)){


        MoveDirection = new Vector3( 0 , ClimbSpeed , 0);

}
}

if ( on == true){


if(Input.GetKey(KeyCode.DownArrow)){


        MoveDirection = new Vector3( 0 , -ClimbSpeed , 0);

}
}


			MoveDirection.y -= Gravity * Time.deltaTime;
			
						var Controller = GetComponent(CharacterController);
						
							var Flags = Controller.Move( MoveDirection * Time.deltaTime);
							
									grounded = (Flags & CollisionFlags.CollidedBelow) !=0;
									
									if (on == true){
									
									   grounded = (Flags & CollisionFlags.CollidedBelow) !=1;
									
									}
									
									if (on == false){
									
									   grounded = (Flags & CollisionFlags.CollidedBelow) !=0;
									
									}
}




function HeadBobbing (){



	if(HeadBobOn == true){
		
		
		  var BobAmount = Mathf.Min(0.5, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));
		  
		var NexPositionX = localPosition.x + headBobDistance.x * Mathf.Sin(Time.time * HeadBobSpeed + Mathf.PI/2);
		  
		  HeadBobTransform.localPosition.x = (localPosition.x * (1 + BobAmount)) + ( NexPositionX *(BobAmount));
	     
  
		  
		  var NexPositionY = localPosition.y + headBobDistance.y * Mathf.Sin(Time.time * HeadBobSpeed + Mathf.PI/2);
		  
		  HeadBobTransform.localPosition.y = (localPosition.y * (1 - BobAmount)) + ( NexPositionY *(BobAmount));

}

}

function OnTriggerEnter(other : Collider) {
  
     on = true;
     
if(on == true){
if( other.gameObject.tag == "Enter Steps" ){
   // Destroy(other.gameObject);
   Debug.LogWarning("You are in the trigger and are ready to climb");
  //transform.parent = ObjectWall.transform;
   
}
else{

on = false;


}
}
}

function OnTriggerExit(other : Collider) {
   
   on = false;
  if(on == false){
  Debug.LogWarning("You are out of the trigger");
if( other.gameObject.tag == "Enter Steps" ){
   // Destroy(other.gameObject);
  
    // transform.parent = null;
  
   
}
else{

   on = true;
        
}
}
}

Hi There.. Sorry for being a NUBE!! but i am new at UNITY still learning it all... Im trying to use your script for a rope climb, no errors in script, but it just isnt working, not sure what im doing wrong.. are you attaching the script, to the character or object?? and also another NUBE question what is the HEAD BOB TRANSFORM...?

–

to the player as for heafbob delete that :)

–