So im new to c# and have been watching a lot of videos on how to work with it. Ive been working on a 2d game and am trying to make my character move. Ive run the code through the debugger numerous times and it says everything I fine but when I try to move the character, nothing happens.
using System.Collections;
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;
}
}
is there something im missing
Well, there’s obvious error
if (Input.GetButtonDown("jump"))
{
}
{
jump = true;
}
This should be
if (Input.GetButtonDown("jump"))
{
jump = true;
}
May be this helps
Assets/playermvmt.cs(6,12): error CS0246: The type or namespace name ‘CharacterController2D’ could not be found (are you missing a using directive or an assembly reference?)
this is the error message I get
You’re missing an using statement in the beginning of your script. If you’re using this GitHub - prime31/CharacterController2D charater controller 2D, then it should state
using Prime31;
And if other, you should find out the namespace where that script is defined and add appopriate using statement.
That didn’t work, is there a free character controller on the Asset store
Use getAxis not getaxis raw. Also you need to put your jump = true code into your input.getbuttondown {}
Also
public CharacterController2D controller;
is empty. You need to drag and drop your charactercontroller2d in the inspector.
Sounds like you need a ton of basics Unity Learn
In c# classes can be declared inside namespaces. If you want to use some class from some other namespace, you should add using directive in the beginning of your script. You may see there already are some like
using UnityEngine;
Check this manual page on namespaces: Unity - Manual: Namespaces
You must find out in wich namespace your CharacterController2D is declared and add a using directive for it.
I declared a using statement for my player, and now my rigidbody command is asking for an object reference. I tried using player but it told that player does not exist in the current context