Hi i use a character controller and wanted to make my player be able to climb up a pole or shaft.
Basically once triggered that the player has hit a pole or climbable surface (i have just tagged it and use OnControllerColliderHit) i’d like the player to be stuck to the surface simular to Super Mario 64 or Prince of persia when climbing a tree, so that left or right will simply make the player move around the pole/tree as in rotating around it. And up and down will move the player up and down the surface.
Sorry for a basic question but i’m having some trouble with it.
Well my main problem was making it stick to the pole while moving left or right, so that moving right will move anti-colockwise around a pole and make the player stick to it.
I thought maybe i’d need to raycast forward to find the normal of the pole and then rotate my player to match that as well as moving the forward to be attached to the pole but i couldn’t seem to figure it out,
Decide where you’re going to be after you’ve moved right.
Raycast from that position either forward or to the center of the object (if you don’t adjust rotation you’ll definately need to raycast to center of object but doing that will give you incorrect results).
Detect the y normal of the triangle the raycast collided with, adjust the player rotation to be facing the normal.
Sooo… (doubt this works but you can play with it to see if it does)
Vector3 newPosition;
RaycastHit hit;
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
newPosition = transform.position + (Vector3.left * Time.deltaTime);
if ([URL="http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html"]Physics.Raycast[/URL] (newPosition , fwd, out hit, 2))
{
this.transform.position = hit.point;
this.transform.rotation = Quaternion.SetFromToRotation(this.transform.rotation.ToAngleAxis(), new Vector3(this.transform.rotation.ToAngleAxis().x, -hit.normal.y, new Vector3(this.transform.rotation.ToAngleAxis().z);
}
The other way is to calculate the future location of the player and find the “ClosestPointOnBounds” from the object you are currently climbing:
Thanks yeah got that going nice, but he appears to be clipping the pole rather baddly almost half his body is inside the pole at times? How should I handel that?