public bool useCharacterForward = false;
public bool lockToCameraForward = false;
public float turnSpeed = 10f;
public KeyCode sprintJoystick = KeyCode.JoystickButton2;
public KeyCode sprintKeyboard = KeyCode.Space;
public bool useJump = false;
public Vector3 jump;
public float jumpImpulse;
private float turnSpeedMultiplier;
private float speed = 0f;
private float direction = 0f;
private bool isSprinting = false;
private Animator anim;
private Vector3 targetDirection;
private Vector2 input;
private Quaternion freeRotation;
private Camera mainCamera;
private float velocity;
private bool isGrounded;
private Rigidbody rb;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
mainCamera = Camera.main;
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
private void OnCollisionStay(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded && useJump)
{
rb.AddForce(jump * jumpImpulse, ForceMode.Impulse);
isGrounded = false;
}
}
Even if the player is in the air and the even if I’m checking for the ground tag I can still press on space once more while the player is in the air after jumping and it will make a jump and move the player higher in the air.
but I’m setting the isGround to false and the player is much higher above the ground and still pressing space make it jumping in the air while in the air already.
This screenshot show how the ground object is built in the hierarchy. The player have a capsule collider and a rigidbody.
There are 3 floor objects :
I tried this way :
private void OnCollisionStay(Collision collision)
{
if (isGrounded == false)
{
isGrounded = true;
}
}
I tried this :
private void OnCollisionStay(Collision collision)
{
isGrounded = collision.gameObject.tag == "Ground";
}
Still while the player is already in the air after the first jumping I can press space again and the player will jump again and get higher.
The full script : The jumping part is in the Update()
using UnityEngine;
public class MovementTesting : MonoBehaviour
{
public bool useCharacterForward = false;
public bool lockToCameraForward = false;
public float turnSpeed = 10f;
public KeyCode sprintJoystick = KeyCode.JoystickButton2;
public KeyCode sprintKeyboard = KeyCode.Space;
public bool useJump = false;
public Vector3 jump;
public float jumpImpulse;
private float turnSpeedMultiplier;
private float speed = 0f;
private float direction = 0f;
private bool isSprinting = false;
private Animator anim;
private Vector3 targetDirection;
private Vector2 input;
private Quaternion freeRotation;
private Camera mainCamera;
private float velocity;
private bool isGrounded;
private Rigidbody rb;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
mainCamera = Camera.main;
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
private void OnCollisionStay(Collision collision)
{
isGrounded = collision.gameObject.tag == "Ground";
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded && useJump)
{
rb.AddForce(jump * jumpImpulse, ForceMode.Impulse);
isGrounded = false;
}
}
// Update is called once per frame
void FixedUpdate()
{
input.x = Input.GetAxis("Horizontal");
input.y = Input.GetAxis("Vertical");
// set speed to both vertical and horizontal inputs
if (useCharacterForward)
speed = Mathf.Abs(input.x) + input.y;
else
speed = Mathf.Abs(input.x) + Mathf.Abs(input.y);
speed = Mathf.Clamp(speed, 0f, 1f);
speed = Mathf.SmoothDamp(anim.GetFloat("Speed"), speed, ref velocity, 0.1f);
anim.SetFloat("Speed", speed);
if (input.y < 0f && useCharacterForward)
direction = input.y;
else
direction = 0f;
anim.SetFloat("Direction", direction);
// set sprinting
isSprinting = ((Input.GetKey(sprintJoystick) ||
Input.GetKey(sprintKeyboard)) &&
input != Vector2.zero && direction >= 0f && useJump == false);
anim.SetBool("isSprinting", isSprinting);
// Update target direction relative to the camera view (or not if the Keep Direction option is checked)
UpdateTargetDirection();
if (input != Vector2.zero && targetDirection.magnitude > 0.1f)
{
Vector3 lookDirection = targetDirection.normalized;
freeRotation = Quaternion.LookRotation(lookDirection, transform.up);
var diferenceRotation = freeRotation.eulerAngles.y - transform.eulerAngles.y;
var eulerY = transform.eulerAngles.y;
if (diferenceRotation < 0 || diferenceRotation > 0) eulerY = freeRotation.eulerAngles.y;
var euler = new Vector3(0, eulerY, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(euler), turnSpeed * turnSpeedMultiplier * Time.deltaTime);
}
}
public virtual void UpdateTargetDirection()
{
if (!useCharacterForward)
{
turnSpeedMultiplier = 1f;
var forward = mainCamera.transform.TransformDirection(Vector3.forward);
forward.y = 0;
//get the right-facing direction of the referenceTransform
var right = mainCamera.transform.TransformDirection(Vector3.right);
// determine the direction the player will face based on input and the referenceTransform's right and forward directions
targetDirection = input.x * right + input.y * forward;
}
else
{
turnSpeedMultiplier = 0.2f;
var forward = transform.TransformDirection(Vector3.forward);
forward.y = 0;
//get the right-facing direction of the referenceTransform
var right = transform.TransformDirection(Vector3.right);
targetDirection = input.x * right + Mathf.Abs(input.y) * forward;
}
}
}
