Giter Club home page Giter Club logo

2d-character-controller's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

2d-character-controller's Issues

It doesnt work and I get errors

Assets\Movement.cs(2,1): error CS1529: A using clause must precede all other elements defined in the namespace except extern alias declarations
Thats all I get when I tried to start it, can somebody help?

Says i have to fix errors but i have the exact cod pasted onto the script

It tells me to fix compile errors some examples of these errors are Assets\CharacterController2d.cs(11,37): warning CS0649: Field 'CharacterController2D.m_WhatIsGround' is never assigned to, and will always have its default value, Assets\CharacterController2d.cs(13,37): warning CS0649: Field 'CharacterController2D.m_CeilingCheck' is never assigned to, and will always have its default value null, Assets\CharacterController2d.cs(14,38): warning CS0649: Field 'CharacterController2D.m_CrouchDisableCollider' is never assigned to, and will always have its default value null, Assets\CharacterController2d.cs(12,37): warning CS0649: Field 'CharacterController2D.m_GroundCheck' is never assigned to, and will always have its default value null, and Should not be capturing when there is a hotcontrol
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&). Please help me

how do I download

on your tutorial it shows you bringing it in from desktop. how do I put it into desktop?

3 Problems

  1. If the character stands at an slope he slides down.
  2. if i jump 2 platforms as fast as possible at the same time up, the character makes one VERY VERY high jump.
  3. If the character jumps up while staying on an slope he jumps higher.
    Sorry for the bad english its not my Native langugae. I hope you are able to understand the porblems.

Crouch not working

When ever I crouch under a platform then release this happens:
https://i.imgur.com/jgwCai4.png

btw the other sprite is there on purpose

`using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    public CharacterController2D controller;
    public Animator animator;
    public float runSpeed = 40f;

    float horizontalMove = 0f;
    bool jump = false;
    bool crouch = false;

     // Update is called once per frame
     void Update () {

             horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;


             animator.SetFloat("Speed", Mathf.Abs(horizontalMove));

             if (Input.GetButtonDown("Jump"))
             {
                   jump = true;
                   animator.SetBool("IsJumping", true);
             }
             
             if (Input.GetButtonDown("Crouch"))
             {
                   crouch = true;
             } else if (Input.GetButtonUp("Crouch"))
             {
                crouch = false;
             }

      }

      public void OnLanding ()
      {
        animator.SetBool("IsJumping", false);
      }
      public void OnCrouching (bool IsCrouching){
        animator.SetBool("IsCrouching", IsCrouching);
      }
      
      void FixedUpdate ()
      {
               // Move our character
               controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
               jump = false;
      }

}`

`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;
[SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
[SerializeField] private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
[SerializeField] private Transform m_CeilingCheck; // A position marking where to check for ceilings
[SerializeField] private Collider2D m_CrouchDisableCollider; // A collider that will be disabled when crouching

const float k_GroundedRadius = .05f; // Radius of the overlap circle to determine if grounded
private bool m_Grounded = true;            // Whether or not the player is grounded.
const float k_CeilingRadius = .05f; // 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(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
	for (int i = 0; i < colliders.Length; i++)
	{
		if (colliders[i].gameObject != gameObject)
		{
			m_Grounded = true;
			if (!wasGrounded && m_Rigidbody2D.velocity.y < 0)
				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(m_CeilingCheck.position, k_CeilingRadius, m_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 (m_CrouchDisableCollider != null)
				m_CrouchDisableCollider.enabled = false;
		} else
		{
			// Enable the collider when not crouching
			if (m_CrouchDisableCollider != null)
				m_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;
}

}
`

Runs on the spot

The player goes into the running animation so I believe that the player movement script works, however they don't actually move just run on the spot. The script worked on a project I had before (which got corrupted) but now it doesn't. I am on a newer version so could it be that?

inputs don't work

Using my keyboard wouldn't cause the character to move, and even after deleting my version of the player movement script and copy-pasting the script of someone who did it 1:1 with the video I still have no luck. Tried other things like enabling mid-air control and messing with the ground's collision to no avail.

not work

it doesn't work, may it be because unity is no longer the same as it was 4 years ago? (i use google traslate, sorry for english)

Controller seem to cause memory leak

Whenever I tried to use the character controller, it pops up an error saying that a native collection has not been disposed, resulting in a memory leak. Any way to fix this?

P.S. My version of Unity is 2021.3

I get this error

UnassignedReferenceException: The variable m_GroundCheck of CharacterController2D has not been assigned.
You probably need to assign the m_GroundCheck variable of the CharacterController2D script in the inspector.
UnityEngine.Transform.get_position () (at <53407686a67f4d21a4ef710a7828900a>:0)
CharacterController2D.FixedUpdate () (at Assets/Scripts/CharacterController2D.cs:51)

Serialized fields acting up?

When using the script as it is provided, the transforms for ceiling and ground checks, as whell as the layer mask return this error from the editor:

Assets\CharacterController2D.cs(11,43): warning CS0649: Field 'CharacterController2D.groundCheck' is never assigned to, and will always have its default value null

Of course, I have already assign them a transform and setted up the layer masks, but, iven when it's shown on the editor, the error persists. This causes the crouchbehaviour to always be activaded.

I have managed to make it work by un-serializing those lines and setting them to public.

Tested on Unity 2018 .3.11f1

I do not know why or what's wrong with the original tho...
Cheers

Jump and Crouch Not Working

My code:

`using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
CharacterController2D controller2D;
float horizontalMove = 0f;
float runSpeed = 70f;
bool jump = false;
bool crouch = false;
// Update is called once per frame
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 FixedUpdate()
{
    controller2D = GameObject.Find("Player").GetComponent<CharacterController2D>();
    controller2D.move(horizontalMove * Time.fixedDeltaTime, crouch, jump, controller2D.GetOnCrouchEvent());
    
}

}
`

Failed copying

Failed copying file 2D-Character-Controller-master/README.md to Assets/README.md.
UnityEditorInternal.InternalEditorUtility:ProjectWindowDrag(HierarchyProperty, Boolean)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

what should i do??

Thx

Unwanted extra jumps

Previously, I had a problem with the character performing infinite jumps once he is off the ground. Another poster some time back added: Cancel Former Momentum on Jump to the Character Controller script. This helped the issue. The character can no longer jump infinitely, but if I rapidly press the button while he is in the air, he will get an extra couple of jumps in there. However, these are not smooth double jumps, they're more like little upward thrusts that last as long as the character is airborne.

This is probably a pretty basic issue, but if someone could help me out with this, I'd appreciate it.

Jump Not Working

So I have all of the code correctly written, I am not using the crouch so I left that part out, but yesterday when I implemented the movement, the jump worked. How can I fix this?
15765421771936651009359225108738

problem

when I paste it, I get errors. I don't know how this works so HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Nvm

I copied the code into unity and it came up with this do you have any solution? Thanks in advance

Hi ! jumping and crouch aren't working.

Hi, first thank for your tutorials. its very good.
bout the character 2d movement, here is a problem I meet, I effectivly press up and down arrows or 'w' and 's' ... or space (I've set everything right about the input), but the caracter dosen t jump or crouch. I put a Debug Log inside the if(Input.Get ... ) and it seems that my key are pressed. So I wonder what's going on.

see below

if (Input.GetButtonDown("Jump"))
{
Debug.Log("Jumped");
jump = true;
}
if (Input.GetButtonDown("Crouch"))
{
Debug.Log("Crouched");
crouch = true;
}
else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}

have you got any idea ?

old code I guess... here are some problems I couldn't figure out

Assets\scripts\CharacterController2D.cs(27,9): error CS0246: The type or namespace name 'UnityEvent' could not be found (are you missing a using directive or an assembly reference?)

Assets\scripts\CharacterController2D.cs(30,27): error CS0246: The type or namespace name 'UnityEvent<>' could not be found (are you missing a using directive or an assembly reference?)

Player jumping more than it should

If the player is going up in a diagonal plataform it jumps way too much. This happens because the jump force is added in the already accumulated up force from the walking up. Most times you don't really want it. I'm working in a fix for it, pull request gonna be up in some hours.

!crouch seemed to be broken

the first !crouch in the if statement seems to be backwards. it should just be 'crouch' because we are normally setting it to false.

Associated Script can not be loaded

If i put the Script on the Player it says the associated Script can not be loaded. That also happens withe every other Game Controller i try.

Unable to add component

Unity spat out an error when I tried to add the component to an object:
"Can't add script component 'CharadterController2D' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match"
I do not know what I did wrong since I downloaded the file and tried putting it on an object directly. Can anybody help?

Quick Question

this isn't really an issue, it's just me being dumb, but what are the controls for this, is it WASD, arrow keys, or do I configure them?

Can't get it in Unity

when I pull it to Unity there appears an error what says: !The referenced script (Unknown) on this Behaviour is missing!
How can I fix that?
The referenced script (Unknown) on this Behaviour is missing! unity 2D-Character-Controller

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.