Hello, i know my titles was f***** but as you guys see i need some help!
I have made a simple script which adds a force to a gameobject using rigidbody (Left, right and up(JUMP))
The problem i have encounter is that when i’m in mid air i can still add a force to the object, which i want to disable. I still want the movement to be the same, i would like to “not be able” to add another force and destroy the path it is taking.
Thanks. My english sucks, please do not be some nazi grammars.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Movement : MonoBehaviour
{
public float maxSpeed = 10f;
public float speed = 100f;
public Rigidbody rb;
public float jumpHeight = 50f;
public float rayLenght = 0.6f;
[SerializeField]
private bool jump;
[SerializeField]
private bool isGrounded;
private float horizontal;
private RaycastHit hit;
void Update()
{
horizontal = Input.GetAxis("Horizontal");
if (Input.GetButtonDown("Jump"))
{
jump = true;
}
CheckIfGrounded();
}
void FixedUpdate ()
{
rb.AddForce(horizontal * speed, 0f, 0f);
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
if (jump && isGrounded)
{
rb.AddForce(0f, 100.0f * jumpHeight, 0f);
jump = false;
isGrounded = false;
}
}
void CheckIfGrounded()
{
if (Physics.Raycast(transform.position, Vector3.down, out hit, rayLenght))
{
if (hit.transform.CompareTag("Ground"))
{
isGrounded = true;
}
}
}
}