- Home /
falling off 90 degree angles when walking on walls
im creating a walk on walls script. I have been using and checking other posts and questions but I really cant seem to figure this one out. ive got it working PERFECTLY on everything but angles 90 degrees or greater.
public float Gravity = 9.81f;
public float JumpForce = 4.5f;
public float GroundMargin = 0.05f;
public KeyCode JumpKey = KeyCode.Space;
public bool IsGrounded;
private float ColliderCenterToEdge;
private Vector3 MyNormal;
private RaycastHit GetNormal;
void Start (){
Rigidbody body = GetComponent<Rigidbody> ();
CapsuleCollider collider = GetComponent<CapsuleCollider> ();
body.freezeRotation = true;
body.useGravity = false;
ColliderCenterToEdge = collider.bounds.extents.y - collider.center.y;
Physics.Raycast (transform.position, -transform.up, out GetNormal);
MyNormal = GetNormal.normal;
IsGrounded = GetNormal.distance <= ColliderCenterToEdge + GroundMargin;
transform.rotation = Quaternion.FromToRotation (Vector3.up, MyNormal);
}
void FixedUpdate(){
Rigidbody body = GetComponent<Rigidbody> ();
IsGrounded = GetNormal.distance <= ColliderCenterToEdge + GroundMargin;
Physics.Raycast (transform.position, -transform.up, out GetNormal);
MyNormal = GetNormal.normal;
body.AddForce (-Gravity * body.mass* MyNormal);
transform.rotation = Quaternion.FromToRotation (Vector3.up, MyNormal);
if (Input.GetKeyDown (JumpKey) & IsGrounded) {
body.velocity += JumpForce * MyNormal;
}
}
im working towards walking on walls is what I should've typed. it only jumps... but its all my code and im pretty proud of it!!! :) but... I need help now. if you attatch this to a rigidbody capsule collider capsule then it works okay but if you hit play and go into scene view and pull the capsules local forward arrow it falls straight down if youre on a cube and drag it past its side. if you do it on a sphere though it follows its contour perfectly. what should I do? I guess I dont think all my levels or any will have perfect 90 degree angles but it would be something good to work out IMO just in case. any ideas? thank you for the help. :)
would sphereCast work? I tried it earlier but I didn't work the way I wanted. I checked the manual too. :( maybe I did it wrong.
separate question that doesn't need an answer. what is the point of having a capsule cast and sphere cast?