# How would I make a rigidbody object climb a wall?

I’m fairly new at Unity and still learning scripting so there’s a lot I might be missing here, but I’m making a 2D game using multiple characters that move by themselves. Therefore I need to program them to behave in certain ways when they interact with certain objects. So far all I’ve been able to accomplish is getting them to start walking when they touch the ground (using a raycast).

My next challenge is getting them to climb when they collide with a “tree trunk”. I can get the object to recognize that it’s touching a tree trunk and it looks like it’s attempting to move up, but something is stopping it and I’m not sure why. Any help would be appreciated. Here is the code I have so far:

var moveSpeed = 5;
var distToGround : float;
var squirrelDirection : boolean;
var climbSpeed = 5;

function Start()
{

``````//get the distance to the ground
distToGround = collider.bounds.extents.y;
``````

}

//Checks distance to ground

function IsGrounded(): boolean
{

``````return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
``````

}

function Update()
{

``````//Starts squirrel moving to the right when it touches the ground
if(IsGrounded() && squirrelDirection == false)
{
//moves squirrel to the right
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
}

if(IsGrounded() && squirrelDirection == true)
{
//moves squirrel to the left
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
}
``````

}

//Clicking on a squirrel changes its direction

function OnMouseDown()
{
if(squirrelDirection == false)
{
squirrelDirection = true;
}

``````else if(squirrelDirection == true)
{
squirrelDirection = false;
}
``````

}

//This is where I’m having the problem

//Climbing function

function OnCollisionEnter ( other : Collision)
{

``````if(other.gameObject.CompareTag("treeTrunk"))
{
var normal = other.contacts[0].normal;

if(normal.x > 0)
{
useGravity = false;
transform.Translate(Vector3.up * climbSpeed * Time.deltaTime);
Debug.Log("Touching tree trunk");
}
}
``````

}

Following up on my comment above, here is a quick hack that should get you close.

All I’m doing is turning `isClimbing` true and false based on when the player collides w/ the tree and then exits that collision. The actual translate happens in Update(). You can see where I use `isClimbing` in Update to determine both the direction and the speed of translation.

``````function OnCollisionEnter (other : Collision) {
if(other.gameObject.CompareTag("treeTrunk")){
var normal = other.contacts[0].normal;
if(normal.x > 0){
isClimbing = true;
}
}
}

function OnCollisionExit (other : Collision) {
if(other.gameObject.CompareTag("treeTrunk")){
isClimbing = false
}
}

function Update(){
var direction = (isClimbing)?Vector3.up:Vector3.forward;
var speed = (isClimbing)?climbingSpeed:moveSpeed;

// if 'squirrelDirection' is true, move right or up, else move down or left.
direction *= (squirrelDirection)?1:-1;

if(/*whatever input you use for movement is true*/)
transform.Translate(direction*speed*Time.deltaTime);
}
``````