UnityNinja Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

UnityNinja CommunityLog in

UnityNinja - Video Game Development Community, including Resources, Forums, Marketplace & More.


descriptionDisable Jump more than 1 time EmptyDisable Jump more than 1 time

more_horiz
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<Rigidbody>().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<Rigidbody>().MovePosition(GetComponent<Rigidbody>().position + transform.TransformDirection(moveAmount) * Time.fixedDeltaTime);
}

descriptionDisable Jump more than 1 time EmptyRe: Disable Jump more than 1 time

more_horiz
i made some pics of the inspector, maybe this helps a bit:

Disable Jump more than 1 time Spiele10
Disable Jump more than 1 time Welt10

descriptionDisable Jump more than 1 time EmptyRe: Disable Jump more than 1 time

more_horiz
Just solved the Problem:

Code:



private void Update()
    {
        Ray ray = new Ray(transform.position, -transform.up);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 1.0f))
            grounded = true;
        else
            grounded = false;

        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<Rigidbody>().AddForce(transform.up * jumpForce);
        }
    }

descriptionDisable Jump more than 1 time EmptyRe: Disable Jump more than 1 time

more_horiz
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply