Hey,
i’m new to the unity engine and have a sphere to walk on. the gravity is pulling the player onto the ground. You can watch by moving the mouse around.
I wanted to make the player being able to jump just 1 time. So i used the if statement to disable jumping again while jumping. But it doesn’t work, even with if, i can jump whenever i want to.
Can some1 solve this =?
Here my code:
public float mouseSensitivityX = 250f;
public float mouseSensitivityY = 250f;
public float walkSpeed = 4f;
public float jumpForce = 220;
public LayerMask groundedMask;
private Transform cameraT;
private float verticalLookRotation;
private Vector3 moveAmount;
private Vector3 smoothMoveVelocity;
private bool grounded;
private void Start()
{
cameraT = Camera.main.transform;
}
private void Update()
{
transform.Rotate(Vector3.up * Input.GetAxis(“Mouse X”) * Time.deltaTime * mouseSensitivityX);
verticalLookRotation += Input.GetAxis(“Mouse Y”) * Time.deltaTime * mouseSensitivityY;
verticalLookRotation = Mathf.Clamp(verticalLookRotation, -60, 60);
cameraT.localEulerAngles = Vector3.left * verticalLookRotation;
Vector3 moveDir = new Vector3(Input.GetAxisRaw(“Horizontal”), 0, Input.GetAxisRaw(“Vertical”)).normalized;
Vector3 targetMoveAmount = moveDir * walkSpeed;
moveAmount = Vector3.SmoothDamp(moveAmount, targetMoveAmount, ref smoothMoveVelocity, .15f);
if (Input.GetButtonDown(“Jump”))
{
if (grounded)
{
GetComponent().AddForce(transform.up * jumpForce);
}
}
grounded = false;
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1 + .1f + groundedMask))
{
grounded = true;
}
}
private void FixedUpdate()
{
GetComponent().MovePosition(GetComponent().position + transform.TransformDirection(moveAmount) * Time.fixedDeltaTime);
}