Hello there
, I recently started the long journey of learning C#, and after a few hours of watching and reading tutorials and digging into the Standard Assets controller scripts, I came up with my very first code prototype, which is supposed to be a 2.5D platformer character controller. It works decently well, the character can move left, right and jump. However, as far as my jump system is concerned, I included a bool called isGrounded that is required for the player to jump, and is set to true when a collider on the character encounters the ground. It works well, however, when I duplicate the cube that I use as a protoype floor to extend the initial platform, this second section does not trigger the collider ! I can’t figure out why… Here’s the code code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
Rigidbody Rgbd;
Animator animCtrl;
CapsuleCollider p_Collider;
public float speed; //setting up floats
public float altitude;
public float jumpForce;
public float moveForcef;
public float hInput;
public bool isGrounded; //setting up bools
private bool isRunning;
private bool isJumping;
private bool isCrouching;
void Start () //Au démarrage...
{
Rgbd = GetComponent<Rigidbody> (); //.cache rigidbody and animator data
animCtrl = GetComponent<Animator> ();
isGrounded = false; //by default, not grounded
}
void OnTriggerEnter () //on platform collide -> grounded
{
isGrounded = true; //grounded on
Debug.Log ("Trigger enter");
}
void OnTriggerExit () //opposite
{
isGrounded = false;
Debug.Log ("Trigger leave");
}
void FixedUpdate () //each frame
{
float hInput = (Input.GetAxis("Horizontal"));
Rgbd.AddForce (hInput * Vector2.right *moveForce); //when input, apply force
if (isGrounded && Input.GetKeyDown(KeyCode.Space)) //if space pressed + grounded
{
Rgbd.AddForce (jumpForce * transform.up); //apply force up
Debug.Log ("Force applied");
}
else //and if not grounded
{
//nothing happens
}
}
}
I apologize in advance for how clumsy this code probably is, since this is my first attempt ever… but if anyone has an idea why the collider doesn’t happen to work out of the initial platform, I’d be relieved ! Thank you very much in advance
NB : I already set up the colliders and rigidbody