Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » Disable Jump more than 1 time

Disable Jump more than 1 time

View previous topic View next topic Go down  Message [Page 1 of 1]

1 Disable Jump more than 1 time on Sun May 08, 2016 12:06 pm

Garzec


Member [Lv. 2]
Member [Lv. 2]
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);
}

View user profile

2 Re: Disable Jump more than 1 time on Sun May 08, 2016 2:12 pm

Garzec


Member [Lv. 2]
Member [Lv. 2]
i made some pics of the inspector, maybe this helps a bit:


View user profile

3 Re: Disable Jump more than 1 time on Sun May 08, 2016 2:58 pm

Garzec


Member [Lv. 2]
Member [Lv. 2]
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);
        }
    }

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum