Comments (15)
I found a solution, if you change the FixedUpdate section of the CharacterController2D script to a LateUpdate it eliminates the issue with jumping.
from 2d-character-controller.
To fix the jump make the ground check and the ceiling check a child of the player, this worked for me
from 2d-character-controller.
I've spent hours and hours trying to get this to work as a straight up beginner. I've copied the code exactly. 2019.1.5f1 version. I was able to troubleshoot that somewhere the code is causing Crouch to be always on. Changing from Serialize to Public as suggested above simply flips my problem and I'm able to crouch and uncrouch but not jump.
Also, most likely the source of all of the issues is somewhere in the code for detecting if a player is crouching under a ceiling. It is ineffective. When I can get crouching working, the code is completely ignored and the player character just stands up into the overhead block and cannot move. This is literally the worst introduction to Unity. Is it always like this? This is barely any code at all and apparently different update versions provide different results.
from 2d-character-controller.
Check if you've already assigned the gameObjects "GroundCheck" and "CeilingCheck" to the controller script.
from 2d-character-controller.
hi ! thank for your reply :) I 've let this pb aside for a couple of days. but yes, my ceiling and groundCheck are positionned correctly. I continue to investigate . Once again, thank you for your help PlzDntBlm.
from 2d-character-controller.
hey, I'm having a similar problem to this. I've triple checked my Ceiling/ground Checks. used Debug.Log to make sure that my "Input.GetKeyDown("f")" is working (It does) and I have no clue what's wrong
code:
bool Crouching = false;
void Update ( ) {
if (Input.GetKeyDown("f"))
{
Crouching = true;
} else if (Input.GetKeyUp("f"))
{
Crouching = false;
}
}
void FixedUpdate()
{
Contoler.Move(HorizontalMove * Time.fixedDeltaTime * MoveSpeed * 80, Crouching, Jump);
Jump = false;
}
also, on another note. my box collider (the one that is to be disabled) is constantly flickering, on and off.
from 2d-character-controller.
Hi! Had a problem too when setting the checks for ceiling and ground. The editor always returned me that they where not declared, and they'll always return null. Just removed the "serializeField" and turn the variable to public. That solved my problem. Here you have my edited script.
`
using UnityEngine;
using UnityEngine.Events;
public class CharacterController2D : MonoBehaviour
{
[SerializeField] private float m_JumpForce = 400f; // Amount of force added when the player jumps.
[Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f; // How much to smooth out the movement
[SerializeField] private bool m_AirControl = false; // Whether or not a player can steer while jumping;
public LayerMask whatIsGround; // A mask determining what is ground to the character
public Transform groundCheck; // A position marking where to check if the player is grounded.
public Transform ceilingCheck; // A position marking where to check for ceilings
public Collider2D crouchDisableCollider; // A collider that will be disabled when crouching
const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
private bool m_Grounded; // Whether or not the player is grounded.
const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
private Rigidbody2D m_Rigidbody2D;
private bool m_FacingRight = true; // For determining which way the player is currently facing.
private Vector3 m_Velocity = Vector3.zero;
[Header("Events")]
[Space]
public UnityEvent OnLandEvent;
[System.Serializable]
public class BoolEvent : UnityEvent<bool> { }
public BoolEvent OnCrouchEvent;
private bool m_wasCrouching = false;
private void Awake()
{
m_Rigidbody2D = GetComponent<Rigidbody2D>();
if (OnLandEvent == null)
OnLandEvent = new UnityEvent();
if (OnCrouchEvent == null)
OnCrouchEvent = new BoolEvent();
}
private void FixedUpdate()
{
bool wasGrounded = m_Grounded;
m_Grounded = false;
// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
// This can be done using layers instead but Sample Assets will not overwrite your project settings.
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, k_GroundedRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
}
}
public void Move(float move, bool crouch, bool jump)
{
// If crouching, check to see if the character can stand up
if (!crouch)
{
// If the character has a ceiling preventing them from standing up, keep them crouching
if (Physics2D.OverlapCircle(ceilingCheck.position, k_CeilingRadius, whatIsGround))
{
crouch = true;
}
}
//only control the player if grounded or airControl is turned on
if (m_Grounded || m_AirControl)
{
// If crouching
if (crouch)
{
if (!m_wasCrouching)
{
m_wasCrouching = true;
OnCrouchEvent.Invoke(true);
}
// Reduce the speed by the crouchSpeed multiplier
move *= m_CrouchSpeed;
// Disable one of the colliders when crouching
if (crouchDisableCollider != null)
crouchDisableCollider.enabled = false;
}
else
{
// Enable the collider when not crouching
if (crouchDisableCollider != null)
crouchDisableCollider.enabled = true;
if (m_wasCrouching)
{
m_wasCrouching = false;
OnCrouchEvent.Invoke(false);
}
}
// Move the character by finding the target velocity
Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
// And then smoothing it out and applying it to the character
m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
// If the input is moving the player right and the player is facing left...
if (move > 0 && !m_FacingRight)
{
// ... flip the player.
Flip();
}
// Otherwise if the input is moving the player left and the player is facing right...
else if (move < 0 && m_FacingRight)
{
// ... flip the player.
Flip();
}
}
// If the player should jump...
if (m_Grounded && jump)
{
// Add a vertical force to the player.
m_Grounded = false;
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
}
private void Flip()
{
// Switch the way the player is labelled as facing.
m_FacingRight = !m_FacingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}`
from 2d-character-controller.
Fixed it temporarily with LateUpdate
, but this seems a bit hacky.
The IsJumping
param gets changed back to false
as soon as the jump physics kick in.
I suspect that this is due to the fact that withing the first few frames the GroundCheck transform is still within the radius specified by k_GroundedRadius
. I imagine that the race condition could be fixed with a bit of state in the CharacterController2D class or even better, determining if the player character just started the jump based on the velocity vector?
Putting this here because it'll take me a while to code a proper solution but you might be able to apply fixes to your code sooner (I'm a software engineer but I'm not well versed in Unity).
from 2d-character-controller.
Ha, someone already submitter a PR, sweet! #3
from 2d-character-controller.
also, on another note. my box collider (the one that is to be disabled) is constantly flickering, on and off.
I know this is a bit late but to fix this make sure you're not setting ground to be on the same layer as anything like the camera. Changed this and it fixed it.
from 2d-character-controller.
I moved my ceiling check up a little more and it then worked. Im using a tilepalette environment with the auto box colliders on painted elements. Good luck!
from 2d-character-controller.
Es klappt nicht zu Springen
Hier mein code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour{
public CharacterController2D controller;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
bool crouch = false;
void Update(){
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
if (Input.GetButtonDown("jump"))
{
jump = true;
}
if (Input.GetButtonDown("crouch"))
{
crouch = true;
}else if (Input.GetButtonUp("crouch"))
{
crouch = false;
}
}
void LateUpdate(){
controller.Move(horizontalMove * Time.deltaTime, crouch, jump);
jump = false;
}
}
from 2d-character-controller.
how to fix it when u press the play button it deletes your box collider without pressing the crouch button? Plzzz help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController2D controller;
public float runSpeed = 40f;
bool jump = false;
bool crouch = false;
float horizontalMove = 0f;
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
if (Input.GetButtonDown("Jump"))
{
jump = true;
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
}
else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
}
private void LateUpdate()
{
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
}
from 2d-character-controller.
I tried all of the recommendations on this post but still I cant get it to jump.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController2D controller;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
if (Input.GetButtonDown("Jump"))
{
jump = true;
}
}
void FixedUpdate()
{
controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
jump = false;
}
}
And for the ceiling/ground check I have no idea where to place them
from 2d-character-controller.
This is very late but after some time of fidgeting with it I figured out that you gotta keep the ground object in a different layer than the player which was what I was doing and that ground check and ceiling check in the same layer as ground.
from 2d-character-controller.
Related Issues (20)
- !crouch seemed to be broken
- old code I guess... here are some problems I couldn't figure out HOT 1
- Crouch not working
- Double or Triple Jump, also A charged Jump and Dash needed HOT 1
- Says i have to fix errors but i have the exact cod pasted onto the script HOT 1
- cant get it in to unity HOT 1
- Runs on the spot
- Can't get it in Unity HOT 1
- I get this error HOT 1
- Associated Script can not be loaded HOT 1
- Nvm
- I am not able to put it into player
- Controller seem to cause memory leak
- error message poped up :/ HOT 1
- Unwanted extra jumps
- not work HOT 1
- Jump and Crouch Not Working HOT 1
- OverlapCircleAll allocates an array 60 times per second!
- Sketches
- problem4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 2d-character-controller.