Question by
Angorin_Studios · Feb 06, 2019 at 01:39 AM ·
jumpingrigidbody.addforce
Need helping figuring out why most standard "Jump" methods do not work for my script
Below is my code. rb.addforce didnt work, none of the other methods ive seen online have worked, im totally lost. I'm a newb programmer, but normally i'll rack my brain for days and days and figure it out, but this time i'm totally lost. Help?
public class player : MonoBehaviour { private Rigidbody rb; public float Rvelocity = 0; public float velocity = 0; public float turnSpeed = 0; public float height = 0.5f; public float heightPadding = 0.05f; public LayerMask ground; public float maxGroundAngle = 120.0f; float groundAngle; Vector3 forward; RaycastHit hitInfo; bool grounded; public float jumpForce = 0;
public CapsuleCollider col;
Vector2 input;
float angle;
Quaternion targetRotation;
Transform cam;
private void Start()
{
rb = GetComponent<Rigidbody>();
col = GetComponent<CapsuleCollider>();
cam = Camera.main.transform;
}
private void Update()
{
GetInput();
CalculateDirection();
CalculateForward();
CalculateGroundAngle();
CheckGround();
//ApplyGravity();
if (Mathf.Abs(input.x) < 1 && Mathf.Abs(input.y) < 1) return;
Rotate();
Move();
}
void Jump()
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
void GetInput()
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
if (Input.GetKeyDown(KeyCode.Space))
{
if(grounded)
{
Jump();
}
else
{
Debug.Log("You must be grounded in order to jump again");
return;
}
}
}
void CalculateDirection()
{
angle = Mathf.Atan2(input.x, input.y);
angle = Mathf.Rad2Deg * angle;
angle += cam.eulerAngles.y;
}
void Rotate()
{
targetRotation = Quaternion.Euler(0, angle, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
}
void Move()
{
if (groundAngle >= maxGroundAngle) return;
if (Input.GetKey("left shift"))
{
transform.position += forward * Rvelocity * Time.deltaTime;
}
else
{
transform.position += forward * velocity * Time.deltaTime;
}
}
void CalculateForward()
{
if (!grounded)
{
forward = transform.forward;
return;
}
forward = Vector3.Cross(transform.right, hitInfo.normal);
}
void CalculateGroundAngle()
{
if (!grounded)
{
groundAngle = 90.0f;
return;
}
groundAngle = Vector3.Angle(hitInfo.normal, transform.right);
}
void CheckGround()
{
if (Physics.Raycast(transform.position, -Vector3.up, out hitInfo, height + heightPadding, ground))
{
if (Vector3.Distance(transform.position, hitInfo.point) < height)
{
transform.position = Vector3.Lerp(transform.position, transform.position + Vector3.up * height, 5 * Time.deltaTime);
}
grounded = true;
}
else
{
grounded = false;
}
}
//void ApplyGravity()
//{
// if (!grounded)
// {
// transform.position += Physics.gravity * Time.deltaTime;
// }
//}
}
Comment