- Home /
Character controller wall climb mechanic in C#
Hey all,
So I have spent about a week researching how to get this to work correctly, and now I have decided to reach out to the world for support.
I am working on a passion project that is a third person puzzle solver. Nothing fancy, nothing extreme. I have my third person character controller I have been working on for the past two weeks that involves a controller script and motor script.
I have everything working EXACTLY how I want it, but now I have decided to try and make a mechanic that uses a raycasthit forward to find a gameobject with a tag that lets the player climb it when the raycast reads the normal of the gameobject. This all works perfectly.
Now my problem is this:
When I set the rotation to the normal of the object with a quaternion it does just that, except for a couple things.
1-When the player is facing -Z in world space on an object it flips my character in the X axis, so hes upside down.
2-I have the character controller snapping to face the forward normal of the gameobject, but after he moves to the new location (which is 3 up in Y and .5 forward ( to the normal) of the gameobject) if you hit the Rotate that I setup in the axis input the character snaps back to the old rotation before the script made it face the normal.
Here's the working parts for this in my scripts my script. If anyone has any ideas, please let me know!
void FixedUpdate()
{
RaycastHit hitForward;
float maxDistance = .2f;
WallGrabDirectionForward = Vector3.zero;
Vector3 castFwd = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(rayCastObj.transform.position, castFwd * 5, Color.green);
if(Physics.Raycast(rayCastObj.transform.position, transform.forward, out hitForward, maxDistance))
{
WallGrabDirectionForward = new Vector3 (hitForward.normal.x, hitForward.normal.y, hitForward.normal.z);
print(hitForward.normal);
print(hitForward.collider);
canClimb = hitForward.collider.gameObject;
if(canClimb.tag == "CanClimb")
{
print("YOU CAN CLIMB");
CanGrab = true;
}
}
else
{
CanGrab = false;
Grabbing = false;
}
}
void HandleActionInput()
{
if(Input.GetButtonDown("Jump"))
{
if(CharacterController.isGrounded && CanGrab == true)
{
NewRotation = transform.rotation = Quaternion.FromToRotation(Vector3.forward, -WallGrabDirectionForward);
Input.ResetInputAxes();
TP_Controller.Instance.enabled = false;
charAnim.SetTrigger(climbWall_hash);
Grabbing = true;
StartCoroutine(WallGrab());
}
if(CharacterController.isGrounded && CanGrab == false)
{
//charAnim.ResetTrigger(climbWall_hash);
charAnim.ResetTrigger(fall_hash);
charAnim.SetTrigger(jump_hash);
Jump();
}
}
}
void Jump()
{
TP_Motor.Instance.Jump();
}
IEnumerator WallGrab()
{
yield return new WaitForSeconds(2);
TP_Motor.Instance.WallGrab();
}
public void WallGrab()
{
if(TP_Controller.CharacterController.isGrounded)
{
TP_Controller.Instance.enabled = true;
targetPositionWall = new Vector3(-TP_Controller.WallGrabDirectionForward.x, 3, -TP_Controller.WallGrabDirectionForward.z);
TP_Controller.CharacterController.Move(targetPositionWall);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Grabing edges aproach? 0 Answers
Modify tps standard script to be able to climb pipe/ladder 0 Answers
A simple ladder in C# or Js 3 Answers