#Need help with beginner scripts (MouseLook and KeyboardMove)

1 messages · Page 1 of 1 (latest)

signal trellis
#

Heya everyone, I just joined the server today. I have this class for college which is basically intro to game programming, and the professor I have isn't the greatest because their using Webex, and so, technical difficulties happen on their end and they record their lessons. I've been following how to write the code (since they show how they type it out), but it just doesn't work the way I expect it to.

#

Basically I have to make a simple FPS game, but I'm struggling on the basic keyboard movement and mouse looking script

#

The player character just floats and doesn't move properly relative to how they're positioned.

#
{
    public float speedMultiplier = 1f;
    public float XLookUpperLimit = 0f;
    public float XLookLowerLimit = -40f;

    // Update is called once per frame
    void Update()
    {
        float currentXRotation = transform.localEulerAngles.x;
        float xRotationDelta = -Input.GetAxis("Mouse Y") * speedMultiplier;
        float newXRotation = currentXRotation + xRotationDelta;
        float clampedXRotation = Mathf.Clamp(newXRotation, XLookLowerLimit, XLookUpperLimit);

        float currentYRotation = transform.localEulerAngles.y;
        float yRotationDelta = Input.GetAxis("Mouse X") * speedMultiplier;
        float newYRotation = currentYRotation + yRotationDelta;

        transform.localEulerAngles = new Vector3(newXRotation, newYRotation, 0f);

    }
}```
#

This is for the mouse movement

#
{
    public float PlayerMovementSpeed = 1.0f;
    public float Gravity = -9.8f;

    private CharacterController controller;

    void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    void Update()
    {
        float PlayerHorizontalMovement = Input.GetAxis("Horizontal");
        float PlayerHorizontalMovementSpeed = PlayerHorizontalMovement * PlayerMovementSpeed * Time.deltaTime;

        float PlayerVerticalMovement = Input.GetAxis("Vertical");
        float PlayerVerticalMovementSpeed = PlayerVerticalMovement * PlayerMovementSpeed * Time.deltaTime;

        PlayerVerticalMovementSpeed = Mathf.Clamp(PlayerVerticalMovementSpeed, -PlayerMovementSpeed, PlayerMovementSpeed);

        Vector3 MovementDirection = new Vector3(PlayerHorizontalMovementSpeed, 0, PlayerVerticalMovementSpeed);
        MovementDirection = Vector3.ClampMagnitude(MovementDirection, PlayerMovementSpeed);

        MovementDirection = transform.TransformDirection(MovementDirection);

        controller.Move(MovementDirection);
    }
}
#

This is for the keyboard movement

#

I'm not sure how to approach this

foggy linden
#

If the object has a Character Controller, stuff done to Transform will be ignored.

#

to sync the transform to the CC

#

Should be done after applying activity on Transform

#

ie: cs transform.localEulerAngles = ... Physics.SyncTransforms();

signal trellis
#

Okay, I'll try that

#

I'll update this to say how it goes

foggy linden
#

Reminder that Input.GetAxis acquires a normalized value already. Multiplying by Time.deltaTime is unnecessary - an issue that's occurring all over the internet.

signal trellis
#

I see, I saw my professor do the code like that, so that's why it's there

#

So should I just remove the deltaTime then?

foggy linden
#

It should be valid for keyboard and joystick input, just not mouse.

#

Which is fine for the above.

foggy linden
signal trellis
#

Ah okay

#

Well, I figured out why I wasn't moving properly. It was because I had the MouseLook script on both the camera and the player. Once I removed the component from the camera and left it on the player, I'm able to move properly too. Only problem is that there's no gravity implemented.

#

So if I were to look up and press W, I would move straight up

#

Oh, and I did add the Physics.SyncTransforms() part at the end of the MouseLook code just to be on the safe side

signal trellis
#

So to my understanding, in order for me to fix that gravity problem I have to look at the line that says Vector3 MovementDirection = new Vector3(PlayerHorizontalMovementSpeed, 0, PlayerVerticalMovementSpeed);, because everytime I modify the 0, it then affects it so when I look up or down, the player just zooms.

#

And I know why it's doing that, it's because when I look up or down, the capsule also looks up and down, therefore rotating the shape up or down and it doing a 'pseudo-fall' towards the walls.

#

When in reality, I want the capsule to remain stationary regardless of it's position looking up or down

signal trellis
#

This is basically what's happening

foggy linden
#

How I would implement a simple Character Controller: cs public class PlayerController : MonoBehaviour { [SerializeField] Transform head; [SerializeField] CharacterController body; [SerializeField] float moveSpeed = 25f; [SerializeField] float turnSpeed = 25f; [SerializeField] float pitchLimit = 45f; Vector3 move; float rotY; float rotX; private void Start() { Cursor.lockState = CursorLockMode.Locked; } void Update() { move = body.transform.right * Input.GetAxis("Horizontal") + body.transform.forward * Input.GetAxis("Vertical"); body.Move(move * moveSpeed * Time.deltaTime); body.Move(Physics.gravity * Time.deltaTime); rotY += Input.GetAxis("Mouse X") * turnSpeed / 10f; rotX -= Input.GetAxis("Mouse Y") * turnSpeed / 10f; rotX = Mathf.Clamp(rotX, -pitchLimit, pitchLimit); body.transform.localRotation = Quaternion.Euler(0f, rotY, 0f); head.localRotation = Quaternion.Euler(rotX, 0f, 0f); Physics.SyncTransforms(); } }

signal trellis
#

I wonder, what version of unity are you using? I'm using 2020.3

foggy linden
#

Shouldn't matter much as long as it isn't anything legacy... 2021.3.2f1.