Question by
Foxi · Aug 30, 2020 at 08:05 PM ·
c#scripting problemjumpingbug-perhaps
Can jump even when in the air.
Hi, everything worked as it should, but I'm not sure where, but I guess after I added capsule colliders, because of cloth not clipping through character, my character can jump no matter what, even when script says only on ground. Video . Any ideas how to fix it?
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{
public float speed = 7;
public Rigidbody rb;
public GameObject character;
public bool PlayerOnTheGround = true;
public float smoothMoveTime = .1f;
public float turnspeed = 500;
float angle;
float smoothInputMagnitude;
float smoothMoveVelocity;
void Update()
{
ControllPlayer();
}
void ControllPlayer()
{
Vector3 inputDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0).normalized;
float inputMagnitude = inputDirection.magnitude;
smoothInputMagnitude = Mathf.SmoothDamp(smoothInputMagnitude, inputMagnitude, ref smoothMoveVelocity, smoothMoveTime);
float targetAngle = Mathf.Atan2(inputDirection.x, inputDirection.y) * Mathf.Rad2Deg;
angle = Mathf.LerpAngle(angle, targetAngle, Time.deltaTime * turnspeed * inputMagnitude);
transform.eulerAngles = Vector3.up * angle;
transform.Translate(transform.forward * speed * Time.deltaTime * smoothInputMagnitude, Space.World);
{
if (Input.GetButtonUp("Jump") || Input.GetKeyUp(KeyCode.W) && PlayerOnTheGround)
{
rb.AddForce(new Vector3(0, 300, 0), ForceMode.Impulse);
PlayerOnTheGround = false;
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
PlayerOnTheGround = true;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613010857im_/https://answers.unity.com/themes/thub/images/avi.jpg)