StateMachine variable not able to find the input class when I put dot after stateMachine.
It looks like it is recognizing the input class. Did you mean the Character Controller?
Let’s take a look at your PlayerStateMachine.cs.
Rather than pasting in a screenshot, follow the instructions below for pasting in code directly as text.
- Forum User Guides : How to apply code formatting within your post
Hi Brian, I was mistakenly confused with the character controller variable with the controller variable , Now I understands and it is resolved.
The problem currently is that the player is not moving, although the log is printing on screen. When I am disabling the animator the player is falling in the air instead of staying on the terrain.
Please paste in text rather than screenshot (see link at end of this post on formatting code).
Let’s see your revised Tick() method… stateMachine.transform.Translate() is going to get in a battle with stateMachine.Controller.Move() (The controller, like NavMeshAgent, will snap back attempts at direct movment with position changes and translation)
- Forum User Guides : How to apply code formatting within your post
Sorry Brain, I wasn’t able to pay attention on your previous guidance regarding code setup.
public override void Tick(float deltaTime)
{
Vector3 movement = new Vector3();
movement.x = stateMachine.InputReader.MovementValue.x;
movement.y = 0;
movement.z = stateMachine.InputReader.MovementValue.y;
// stateMachine.transform.Translate(movement * deltaTime);
stateMachine.Controller.Move(movement * stateMachine.MovementSpeed * deltaTime);
if (stateMachine.InputReader.MovementValue == Vector2.zero) { return; }
stateMachine.transform.rotation = Quaternion.LookRotation(movement);
}
Here is the updated tick method, I commented the translate line but the player is still not moving
Let’s add some Debugs to see what’s going on…
In InputReader, in OnMovement()
public void OnMovement(InputAction.CallbackContext context)
{
MovementValue = context.ReadValue<Vector2>();
Debug.Log($"OnMovement - Movement Value = {MovementValue}");
}
And in Tick, replace the commented out line with
Debug.Log($"Tick({deltaTime}), movement = {movement}, adjusted movement = {movement * stateMachine.MovementSpeed * deltaTime}");
One added note on code pasting, the most common thing missed:
The character to denote code is the backwards apostrophe on the key next to the 1/! key on your keyboard. It will look like this ` instead of the forwards apostrophe '.
I took the liberty of editing your previous post to insert the proper ``` characters.
Thanks Brain, for the detailed guidance.
Here is the console which is showing the logs from OnMovement function. But the log from Tick is not printing.
That means that either another state is running (unlikely at this stage), or the PlayerStateMachine hasn’t started the state yet.
In PlayerStateMachine.Start(), you might want something like
void Start()
{
SwitchState(new PlayerTestState(this)); //or whatever the state's name is
}
Thank for the response, I added the given line into the PlayerStateMachine script, but unfortunately the player still moving also there is not log printed from the Tick Method.
[field : SerializeField] public InputReader InputReader { get; private set; }
[field: SerializeField] public CharacterController Controller { get; private set; }
[field: SerializeField] public float MovementSpeed { get; private set; }
void Start()
{
SwitchState(new PlayerMovementState(this));
}
void Update()
{
}
Your Update() method is hiding the base Update() method. You’ll want to remove that.
Thank you Brian for identifying the problem, this works fine now the tick method log is now printing.
Now player is facing towards the direction of key press, but still not moving
Now that’s a puzzler… the movement value should be matching the movement debug from the InputReader…
Paste in the complete InputReader and complete PlayerMovementState now that we’ve got the Tick() running…
Sure, Here is the Input Reader Class
public Vector2 MovementValue { get; private set; }
private PlayerInput playerInput;
private void Start()
{
playerInput = new PlayerInput();
playerInput.Player.SetCallbacks(this);
playerInput.Player.Enable();
}
private void OnDestroy()
{
playerInput.Player.Enable();
}
public void OnLook(InputAction.CallbackContext context)
{
}
public void OnMovement(InputAction.CallbackContext context)
{
MovementValue = context.ReadValue<Vector2>();
Debug.Log($"OnMovement - Movement Value = {MovementValue}");
}
Here is the PlayerMovementState Class
public PlayerMovementState(PlayerStateMachine stateMachine) : base(stateMachine)
{
}
public override void Enter()
{
}
public override void Tick(float deltaTime)
{
Vector3 movement = new Vector3();
movement.x = stateMachine.InputReader.MovementValue.x;
movement.y = 0;
movement.z = stateMachine.InputReader.MovementValue.y;
Debug.Log($"Tick({deltaTime}), movement = {movement}, adjusted movement = {movement * stateMachine.MovementSpeed * deltaTime}");
stateMachine.Controller.Move(movement * stateMachine.MovementSpeed * deltaTime);
if (stateMachine.InputReader.MovementValue == Vector2.zero) { return; }
stateMachine.transform.rotation = Quaternion.LookRotation(movement);
}
public override void Exit()
{
}
Ok, we’re now in impossible territory, there is no immediately apparent reason that the Tick()'s movement doesn’t match the InputReader.MovementValue…
Zip up your project and upload it to https://gdev.tv/projectupload and I’ll take a look.
Be sure to remove the Library folder to conserve space.
Thank you for the efforts, Brian. I have uploaded the project.
Lots of head scratching… but then a closer look at the custom model and animations yielded unfortunate results… The model and it’s animations contain root motion curves that are incompatible with a Character Controller setup, and simply will not work for this course.
I recommend following the course instructions on bringing in the Mixamo assets or finding virtually any Humanoid model (model that takes Humanoid animations) and associated animations in the asset store.
Hi Brian, Thank you very much for taking out time to review the project. I created those animation clips myself in blender. Can you suggest what can I change in the animations to make it work with character controller?
The animations are currently generic, because my animation are not working when I change the type to humnoid, although the charachter is humanoid in nature.
I’m utterly useless in Blender… (well, I can do things like remapping low poly models to take in a texture, but nothing with Animation).
We have a new course, taught by the amazing Grant Abbitt on rigging models. https://www.gamedev.tv/courses/2174032 It’s still in progress, but I believe the end goal is models you can port into a game engine like Unity or Unreal and have working with our animation system.
Thank you for providing the course link, Great timing to learn from this course.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.