Hello, I was making a 3D sidescroller and I had a problem with the character’s jumping mechanism. I have no idea how to solve this issue:
Sometimes when the character jumps he gets stuck into something invisible, but if he makes the jump again the jump happens normally.
It’s difficult to explain it, so it will be easier if you just see the script working: just put it in a cube without a box collider and activate the layer “Ignore Raycast” and you will see it happening
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
private float inputDirection; // X Value of MoveVector
private float verticalVelocity; // Y Value of MoveVector
private float speed = 5.0f;
private float gravity = 30.0f;
private float jumpForce = 10.0f;
private bool secondJumpAvail = false;
private Vector3 moveVector;
private Vector3 lastMotion;
private CharacterController controller;
public AudioClip[] audioClip;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
}
void PlaySound(int clip)
{
AudioSource audio = GetComponent<AudioSource>();
audio.clip = audioClip[clip];
audio.Play();
}
// Update is called once per frame
void Update () {
IsControllerGrounded();
moveVector = Vector3.zero;
inputDirection = Input.GetAxis("Horizontal") * speed;
if(IsControllerGrounded())
{
verticalVelocity = 0;
if (Input.GetKeyDown(KeyCode.Space))
{
// Make the player jump
verticalVelocity = jumpForce;
secondJumpAvail = true;
}
moveVector.x = inputDirection;
}
else
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (secondJumpAvail)
{
verticalVelocity = jumpForce;
secondJumpAvail = false;
}
}
verticalVelocity -= gravity * Time.deltaTime;
moveVector.x = lastMotion.x;
}
moveVector.y = verticalVelocity;
//moveVector = new Vector3(inputDirection, verticalVelocity, 0);
controller.Move(moveVector * Time.deltaTime);
lastMotion = moveVector;
}
private bool IsControllerGrounded()
{
Vector3 lefRayStart;
Vector3 rightRayStart;
lefRayStart = controller.bounds.center;
rightRayStart = controller.bounds.center;
lefRayStart.x -= controller.bounds.extents.x;
rightRayStart.x += controller.bounds.extents.x;
Debug.DrawRay(lefRayStart, Vector3.down, Color.red);
Debug.DrawRay(rightRayStart, Vector3.down, Color.green);
if (Physics.Raycast(lefRayStart, Vector3.down, (controller.height / 2) + 0.2f))
return true;
if (Physics.Raycast(rightRayStart, Vector3.down, (controller.height / 2) + 0.2f))
return true;
return false;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if(controller.collisionFlags == CollisionFlags.Sides)
{
if(Input.GetKeyDown(KeyCode.Space))
{
Debug.DrawRay(hit.point, hit.normal, Color.red, 2.0f);
moveVector = hit.normal * speed;
verticalVelocity = jumpForce;
secondJumpAvail = true;
}
}
// Collectables
switch(hit.gameObject.tag)
{
case "Coin":
LevelManager.Instance.CollectCoin();
Destroy(hit.gameObject);
PlaySound(0);
break;
case "Teleporter":
transform.position = hit.transform.GetChild(0).position;
break;
case "WinBox":
LevelManager.Instance.Win();
break;
default:
break;
}
}
}