Hello there. I am quite new to Unity, but, by this moment, I have been able to create some animations for my future game hero. The problem is that when it jumps from one platform to another in 3D space it, sometimes, goes through the upper platform. I use capsule collider for the player and box collider for the platform. What, actually, may be the cause of this problem? I attached a short video of this process as a zip file.
Here is the script which I use for my hero:
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerControl : MonoBehaviour {
[SerializeField] private Animator anim;
[SerializeField] private Rigidbody rb;
[SerializeField] private Transform player;
[SerializeField] private Transform groundCheck;
[SerializeField] private float playerMoveSpeed = 100f;
[SerializeField] private float playerRotateSpeed = 5f;
[SerializeField] private float playerJumpForce = 2000f;
[SerializeField] private bool playerIsGrounded = true;
[SerializeField] private bool playerCanJump = true;
[SerializeField] private bool playerIsWalking = false;
[SerializeField] private double startTime;
[SerializeField] private double duration;
private void Start() {
rb = GetComponent<Rigidbody>();
anim = GameObject.Find("Player").GetComponent<Animator>();
startTime = Time.time;
duration = 10;
}
private void Update() {
if (player.position.y < -100f) {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
if (Physics.OverlapSphere(groundCheck.position, 0.1f).Length == 1) {
playerIsGrounded = true;
}
//GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
rb.constraints = RigidbodyConstraints.FreezeRotation;
if (playerIsGrounded == false) {
rb.constraints &= ~RigidbodyConstraints.FreezePositionY;
anim.SetBool("isJumpingDown", true);
}
Vector2 controlVector = GetPlayerControlVector();
Vector3 moveVector = new Vector3(controlVector.x, 0f, controlVector.y);
playerIsWalking = moveVector != Vector3.zero;
if (playerIsWalking == true) {
startTime = Time.time;
player.position += moveVector * playerMoveSpeed * Time.deltaTime;
player.forward = Vector3.Slerp(player.forward, moveVector, playerRotateSpeed * Time.deltaTime);
anim.SetBool("isWalking", true);
anim.SetBool("isIdleAfterPause", false);
} else {
anim.SetBool("isWalking", false);
if (Time.time - startTime > duration) {
startTime = Time.time;
anim.SetBool("isIdleAfterPause", true);
} else {
anim.SetBool("isIdleAfterPause", false);
}
}
if (Input.GetKeyDown(KeyCode.Space)) {
if (playerCanJump == true) {
rb.constraints &= ~RigidbodyConstraints.FreezePositionY;
Debug.Log("Jump");
Jump();
}
}
if (Input.GetKeyUp(KeyCode.Space)) {
if (playerCanJump == true) {
anim.SetBool("isJumping", false);
rb.constraints = RigidbodyConstraints.FreezePositionY;
}
}
}
private void Jump() {
startTime = Time.time;
Vector2 controlVector = GetPlayerControlVector();
rb.AddForce(new Vector3(controlVector.x * playerMoveSpeed, playerJumpForce, controlVector.y * playerMoveSpeed), ForceMode.Impulse);
anim.SetBool("isJumping", true);
}
private Vector2 GetPlayerControlVector() {
Vector2 controlVector = new Vector2(0, 0);
if (Input.GetKey(KeyCode.A)) {
controlVector.x = -1;
}
if (Input.GetKey(KeyCode.D)) {
controlVector.x = +1;
}
if (Input.GetKey(KeyCode.W)) {
controlVector.y = +1;
}
if (Input.GetKey(KeyCode.S)) {
controlVector.y = -1;
}
return controlVector.normalized;
}
private void OnCollisionEnter(Collision other) {
if (other.gameObject.tag == "Ground") {
Debug.Log("On the Ground");
playerCanJump = true;
anim.SetBool("isJumping", false);
anim.SetBool("isJumpingDown", false);
}
}
private void OnCollisionExit(Collision other) {
Debug.Log("Beyond the Ground");
rb.constraints &= ~RigidbodyConstraints.FreezePositionY;
gameObject.GetComponent<Rigidbody>().useGravity = true;
playerIsGrounded = false;
playerCanJump = false;
if (playerIsGrounded == false) {
anim.SetBool("isJumpingDown", true);
}
}
}
9884244–1426179–3DGameDemo.zip (1.63 MB)