Help centering a player!

Sorry about the title I didnt know what to name the topic, but I need help and I’ve tired to find a solution for something so basic and I cant do it its frustrating me and before i give u on my project i’d rather ask for help. So i’m creating Donkey Kong some have seen my project thread but most probably haven’t in the thread many people have suggested I center the player as he goes up the ladder, I agree and so I’ve been working on this and I can do it once as you can see here

But I run into a problem if I want to do it more than once. I cannot figure out how. I have tried to think of solutions but to no avail I’ve been trying for about 2 hours and I’m getting quite annoyed because I know the answer has to be simple. Any solutions on how I can center the player on each ladder in the game

as you can see I have many ladders in there and I need the player to be centered when climbing that ladder. Please someone just point me in the right direction…

Oh I’d like to point out i’d like to do this without a crap load of variables or tags

do you have colliders attached or anything that can be used to determine size?

Can you show or tell us how you made it work once?

Edit: But in principle it’s really simple. You must already know when he’s climbing a ladder and which one he’s climbing, that’s all you need to know. When he’s climbing a ladder, just set the horizontal component of his position to match the horizontal component of the ladder’s position.

Yeah here it is

Checks to see if the player has collided with the ladder collider

void OnTriggerEnter(Collider other) {

		if(other.gameObject.tag == "Player"){
			canClimb = true;

				}

			}

	void OnTriggerExit(Collider other){
		if(other.gameObject.tag == "Player"){
			climbing = false;
			canClimb = false;

		}
}

This is to make the player climb up the ladder

private void Climb(){

		if(Input.GetKey(KeyCode.UpArrow)  canClimb == true){	
            //Makes the player centered on the first ladder
			Vector3 pos = transform.position;
			pos.x = 1.93f;
			pos.y += climbSpeed * Time.deltaTime;
			transform.position = pos;
			climbing = true;
			gravityForce = 0;

		}	
}

This is my style of coding and it may or may not be the most efficient, basically i have thought to set up tags for each ladder but it wouldn’t work i’ve thought to use variables when he enter a trigger but how do i know which trigger he entered on a level with two ladders. I even thought to use arrays but I dont think that would work, I am stumped…

any suggestions or pushes in the right direction would be great

Edit: I know this

What i dont know is how to do this for each individual ladder

collider has bounds

bounds have a center and a size

I would start there

oh

float x = 0;

switch(ladderNum)
{
case 0:
 x = 1.5f;
break;

case 1:
x = -1f;
break;

case 2:
x = 1.2f;
break;
}

Vector3 pos = transform.position;

pos.x = x;

pos.y += climbSpeed * Time.deltaTime;

transform.position = pos;

climbing = true;

gravityForce = 0;

Thanks alot that should work nicely… I’ve been working on it all day to finaly have a solution is nice :slight_smile:

The above is a better solution.

Assuming ‘other’ (tagged “Player”) is the ladder, and this script is attached to the player:

float xVal=0f;

void OnTriggerEnter (Collider other) {

    if (other.gameObject.tag == "Player") {
        xVal = other.bounds.center.x;
    }

    canClimb = true;

}

void OnTriggerExit (Collider other) {

    climbing = false;
    canClimb = false;

}



//when climbing
Vector3 pos = transform.position;
pos.x = xVal;
pos.y += climbSpeed*Time.deltaTime;
transform.position = pos;
climbing = true;
gravityForce = 0;

I didnt know what jclnz ment by bounds that is so helpful Dman thanks so much… I know from now on… thank you both