I got my keyboard controller working with the new input system.
I then try to make it work onscreen but the gamepad’s left stick does not stop my character nor rotates it properly. I tried searching the web for a very long time but can’t seem to find an answer to fix it.
I would suggest that you poll the value of the move action in the Update() call rather than use events. Then update the rigidbody in the FixedUpdate call. I’ve modified your project to do that and it works for KB, hardware gamepads and the on screen joystick. You can still use events for the other actions. Hope that helps.
I feel kinda sad and happy… I’ve been searching for so long but I couldn’t fix it. Your codes are so simple plus it simplified my rotation codes. Thank you so much, man!
I’m trying to expand the rotation to snap based on the Movements cases but it doesn’t seem to work for me. Any idea?
private void RotateToFaceMovementDirection()
{
if (walkVelocity.x != 0 || walkVelocity.z != 0)
{
var targetRotation = Mathf.Round(Mathf.Atan2(walkVelocity.x, walkVelocity.z) * Mathf.Rad2Deg);
if (targetRotation < 0) { targetRotation += 360; }
// Smoothly rotate towards the moving direction.
if (Movement == Movements.Free)
{
transform.eulerAngles = Vector3.up * Mathf.LerpAngle(transform.eulerAngles.y, targetRotation, rotateSpeed * Time.deltaTime);
}
// Instantly rotate towards the moving direction and account for the selected movement mode.
else if (Movement == Movements.Strict2DirectionsHorizontal)
{
switch (facing)
{
case FacingDirection.East: transform.eulerAngles = new Vector3(0, 90, 0); break;
case FacingDirection.West: transform.eulerAngles = new Vector3(0, 270, 0); break;
}
}
else if (Movement == Movements.Strict2DirectionsVertical)
{
switch (facing)
{
case FacingDirection.North: transform.eulerAngles = new Vector3(0, 0, 0); break;
case FacingDirection.South: transform.eulerAngles = new Vector3(0, 180, 0); break;
}
}
else if (Movement == Movements.Strict4Directions)
{
switch (facing)
{
case FacingDirection.North: transform.localRotation = Quaternion.Euler(0, 0, 0); break;
case FacingDirection.South: transform.localRotation = Quaternion.Euler(0, 180, 0); break;
case FacingDirection.East: transform.localRotation = Quaternion.Euler(0, 90, 0); break;
case FacingDirection.West: transform.localRotation = Quaternion.Euler(0, 270, 0); break;
}
}
else
{
transform.eulerAngles = Vector3.up * Mathf.LerpAngle(transform.eulerAngles.y, targetRotation, rotateSpeed * Time.deltaTime);
}
}
}
Your follow up question isn’t about the new input system so shouldn’t really be here;)
But…If you want to restrict the player movement I would do that when you process your input and leave the rotation as it was. I’ve modified the CharacterAbilityWalk script which hopefully does what you were after. Change the allowed movement type from the inspector to get 2D or 4D or free move behaviour.
Sorry about that… I thought since you already viewed my project it will be better here. Nonetheless, I’m really grateful for your help!
Actually that is not what I’m after. I want the rotations to “snap”. That means no rotations.
public enum Movements { Free, Strict2DirectionsHorizontal, Strict2DirectionsVertical, Strict4Directions, Strict8Directions }
public Movements Movement = Movements.Free;
Free=3D objects (when you send the project back to me ealier that was perfect!)
Strict2DirectionsHorizontal=Old Mario
Strict2DirectionsVertical=Old Space Game
Strict4Directions=Old Tanks Game
Strict8Directions=Like Don’t Starve Together
So it’s like making a single controller that can be used in different game genres.
Ok I see what you’re after. Now you have the input working you should probably post your question into one of the general forums. It might be a bit much to have one controller do everything in both the 2D and 3D worlds. If it’s just 3D with various camera angles then you’re heading in the right direction - you have the basic movements. If you don’t want the player to rotate as in a space invaders type shooter - just don’t call the rotation code.