#Is this how movement in a game is supposed to work?

1 messages · Page 1 of 1 (latest)

modern isle
#

This is the code, but there doesnt seem to be anything weid in it ⁨⁨⁨⁨⁨```cs
Vector2 Direction = MovementV2.ReadValue<Vector2>();
Rigidbody Rigidbody = Character.GetComponent<Rigidbody>();

Vector3 Z = Direction.x * Character.transform.right;
Vector3 X = Direction.y * Character.transform.forward;

Vector3 ForceDirection = (Z + X) * MovementSpeed + Physics.gravity;

Rigidbody.linearVelocity = ForceDirection;```⁩⁩⁩⁩⁩

#

maybe adding the Z+X causes this?

#

Question:Is that how a character moves(im confused 😭 )?

versed field
#

Also the gravity part doesnt seem right

#

But thats unrelated

#

Unless your gravity has non-zero X or Z values

modern isle
modern isle
modern isle
#

its pivoted in the middle of the screen

#

I realized that the character goes a bit to the left

#

How do I fix that now?

pliant cliff
modern isle
pliant cliff
#

This is what Debug.Log was made for

#

We don't need to waste any much time thinking of theoreticals about why, we can just directly check if it's the case and then move on if it isn't

modern isle
#

Is this because im doing Z+X?

pliant cliff
modern isle
pliant cliff
#

which is what

#

ForceDirection?

#

or Direction?

#

I'm talking code specifics here

modern isle
#

The direction I apply to a force

#
  • movement speed
pliant cliff
#

you should print both of those variables separately

#

ForceDirection and Direction

#

and label them

#

and let us know what you see

modern isle
#

This is because im doing X+Z I think

pliant cliff
#

and the console window with the logs

modern isle
#

Direction

#

⁨```Vector2 Direction = MovementV2.ReadValue<Vector2>();
Rigidbody Rigidbody = Character.GetComponent<Rigidbody>();

Vector3 Z = Direction.x * Character.transform.right;
Vector3 X = Direction.y * Character.transform.forward;

Vector3 ForceDirection = (Z + X) * MovementSpeed + Physics.gravity;

Debug.Log(Direction);

Rigidbody.linearVelocity = ForceDirection;```⁩

#

code

pliant cliff
#

this is pretty inefficient. Just do this:

Debug.Log($"Direction: {Direction}, ForceDirection: {ForceDirection}, Forward: {Character.transform.forward}, Right: {Character.transform.right}");```
#

it will save us a lot of time going back and forth

modern isle
pliant cliff
#

(this is what i meant by labels)

pliant cliff
#

it's nothing to do with debug.log really

#

this is just how to make nice strings in C#

#
string log = $"Direction: {Direction}, ForceDirection: {ForceDirection}, Forward: {Character.transform.forward}, Right: {Character.transform.right}";
Debug.Log(log);```
modern isle
#

Do I send the results as an image or as a video

pliant cliff
# modern isle

ok so - the last part of the log is showing us pretty clearly your character is just rotated slightly

modern isle
#

I only moved forward

pliant cliff
#

your character starts out rotated

#

can you show how your character object is set up

#

the hierarchy of it, adn the inspectors for each object in it?

#

I suspect your camera is just rotated slightly or something

modern isle
#

I think it is because I was looking around with the character while walking

pliant cliff
#

ok now go down the line - ViewModelPivot, CameraPivot, and Main Camera

#

are any of those rotated?

#

also how does your camera rotation work? Which object is it rotating?

modern isle
#

I did make a bridge to test it better

#

and it goes to the right

#

let me test something

pliant cliff
#

Let's see the code for these two scripts

modern isle
#

the forward movement it bad I think

modern isle
pliant cliff
pliant cliff
#

yes

#

!code

blissful palmBOT
modern isle
#

⁨```cs //-----Libraries-----\
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
using UnityEngine.WSA;

public class CameraRotation : MonoBehaviour
{
//-----Public Classes-----\
public GameObject ModelPivot;
public GameObject Model;

[Space]

public bool CanTurn = true;

[Space]

[Range(0,20)]
public float TurningSpeed = 20f;
//-----Private Classes-----\\

private float XAngle = 0f;
private float YAngle = 0f;

private InputAction MouseDelta;

private CameraInputs CameraInputs;

private void Awake()
{
    CameraInputs = new CameraInputs { };
}

private void OnEnable()
{
    MouseDelta = CameraInputs.Default.MouseDelta;
    MouseDelta.Enable();
}

private void OnDisable()
{
    MouseDelta.Disable();
}

private void Update()
{
    if (ModelPivot != null && Model != null && CanTurn != false)
    {
        if (UnityEngine.Cursor.lockState != CursorLockMode.Locked)
        {
            UnityEngine.Cursor.lockState = CursorLockMode.Locked;
        }

        Vector2 Axis = MouseDelta.ReadValue<Vector2>();
        XAngle = XAngle + Axis.y * TurningSpeed * Time.deltaTime;
        YAngle = YAngle + Axis.x * TurningSpeed * Time.deltaTime;

        XAngle = Mathf.Clamp(XAngle, -90, 90);

        ModelPivot.transform.rotation = Quaternion.Euler(-XAngle, 0, 0);
        Model.transform.rotation = Quaternion.Euler(0, YAngle, 0);
    }
    else
    {
        if (UnityEngine.Cursor.lockState != CursorLockMode.None)
        {
            UnityEngine.Cursor.lockState = CursorLockMode.None;
        }
    }
}

}

#

I did add cs to the first line

pliant cliff
#

needs to be like:
```cs

code here
```

modern isle
#

⁨```cs
code

pliant cliff
#

but it's fine

modern isle
#

⁨```cs
//-----Libraries-----\
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
using UnityEngine.WSA;

public class CameraRotation : MonoBehaviour
{
//-----Public Classes-----\
public GameObject ModelPivot;
public GameObject Model;

[Space]

public bool CanTurn = true;

[Space]

[Range(0,20)]
public float TurningSpeed = 20f;
//-----Private Classes-----\\

private float XAngle = 0f;
private float YAngle = 0f;

private InputAction MouseDelta;

private CameraInputs CameraInputs;

private void Awake()
{
    CameraInputs = new CameraInputs { };
}

private void OnEnable()
{
    MouseDelta = CameraInputs.Default.MouseDelta;
    MouseDelta.Enable();
}

private void OnDisable()
{
    MouseDelta.Disable();
}

private void Update()
{
    if (ModelPivot != null && Model != null && CanTurn != false)
    {
        if (UnityEngine.Cursor.lockState != CursorLockMode.Locked)
        {
            UnityEngine.Cursor.lockState = CursorLockMode.Locked;
        }

        Vector2 Axis = MouseDelta.ReadValue<Vector2>();
        XAngle = XAngle + Axis.y * TurningSpeed * Time.deltaTime;
        YAngle = YAngle + Axis.x * TurningSpeed * Time.deltaTime;

        XAngle = Mathf.Clamp(XAngle, -90, 90);

        ModelPivot.transform.rotation = Quaternion.Euler(-XAngle, 0, 0);
        Model.transform.rotation = Quaternion.Euler(0, YAngle, 0);
    }
    else
    {
        if (UnityEngine.Cursor.lockState != CursorLockMode.None)
        {
            UnityEngine.Cursor.lockState = CursorLockMode.None;
        }
    }
}

}

pliant cliff
#

i already read it

#

you can share the other script

#

Although there is a bug here

#
            XAngle = XAngle + Axis.y * TurningSpeed * Time.deltaTime;
            YAngle = YAngle + Axis.x * TurningSpeed * Time.deltaTime;```
#

Time.deltaTime should not be here

#

and you should reduce TurningSpeed

modern isle
#

⁨```cs
using UnityEngine;
using UnityEngine.InputSystem;

public class MovementScript : MonoBehaviour
{
//Public\
public GameObject Character;
public GameObject LookPivot;

//Private\\
private Stats Stats;

private float MovementSpeed = 10f;
private float MinusCrouch;
private float PlusRunning;

private PlayerInputs PlayerInputs;
private InputAction MovementV2;

private void Awake()
{
    PlayerInputs = new PlayerInputs();
}

private void OnEnable()
{
    MovementV2 = PlayerInputs.Default.Movement;
    MovementV2.Enable();
}

private void OnDisable()
{
    MovementV2.Disable();
}


void Update()
{

    if (Character != null)
    {
        Vector2 Direction = MovementV2.ReadValue<Vector2>();
        Rigidbody Rigidbody = Character.GetComponent<Rigidbody>();

        Vector3 Z = Direction.x * Character.transform.right;
        Vector3 X = Direction.y * Character.transform.forward;

        Vector3 ForceDirection = (Z + X) * MovementSpeed + Physics.gravity;

        Debug.Log($"Direction: {Direction}, ForceDirection: {ForceDirection}, Forward: {Character.transform.forward}, Right: {Character.transform.right}");

        Rigidbody.linearVelocity = ForceDirection;

    }
}

}

modern isle
modern isle
pliant cliff
#

so you will need to reduce turningspeed

pliant cliff
#

it shouldn't be multiplied by deltaTime

modern isle
pliant cliff
#

it's already "how much did the mouse move this frame"

modern isle
pliant cliff
#

ok honestly the code is looking good to me so I'm not sure other than if you could finish showing me the inspeoctors of those obejcts I asked about above

#

(the gravity thing is definitely still wrong but that's a separate issue)

modern isle
pliant cliff
#

is this a second copy of the camera rotation script?

#

That could be the issue

modern isle
#

The bottom script?

#

This is the main camera

pliant cliff
#

It seems like you have a copy of the "CameraRotation" script on both of these objects:
Character
and
CameraPivot

#

it should only be on Character I think

modern isle
#

I DO

#

I HAVE A COPY

pliant cliff
#

see if removing the second one changes anything

modern isle
#

I think I got another bug

#

I cant turn now

pliant cliff
#

(for the remaining camera rotation script)

modern isle
#

Look

#

I tried to turn right

#

its from this script

#

⁨```cs
//-----Libraries-----\
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
using UnityEngine.WSA;

public class CameraRotation : MonoBehaviour
{
//-----Public Classes-----\
public GameObject ModelPivot;
public GameObject Model;

[Space]

public bool CanTurn = true;

[Space]

[Range(0,2f)]
public float TurningSpeed = 2f;
//-----Private Classes-----\\

private float XAngle = 0f;
private float YAngle = 0f;

private InputAction MouseDelta;

private CameraInputs CameraInputs;

private void Awake()
{
    CameraInputs = new CameraInputs { };
}

private void OnEnable()
{
    MouseDelta = CameraInputs.Default.MouseDelta;
    MouseDelta.Enable();
}

private void OnDisable()
{
    MouseDelta.Disable();
}

private void Update()
{
    if (ModelPivot != null && Model != null && CanTurn != false)
    {
        if (UnityEngine.Cursor.lockState != CursorLockMode.Locked)
        {
            UnityEngine.Cursor.lockState = CursorLockMode.Locked;
        }

        Vector2 Axis = MouseDelta.ReadValue<Vector2>();
        XAngle = XAngle + Axis.y * TurningSpeed;
        YAngle = YAngle + Axis.x * TurningSpeed;

        XAngle = Mathf.Clamp(XAngle, -90, 90);

        ModelPivot.transform.rotation = Quaternion.Euler(-XAngle, 0, 0);
        Model.transform.rotation = Quaternion.Euler(0, YAngle, 0);
    }
    else
    {
        if (UnityEngine.Cursor.lockState != CursorLockMode.None)
        {
            UnityEngine.Cursor.lockState = CursorLockMode.None;
        }
    }
}

}

#

Could it be because YAngle is being updated every second?

#

Tho why arent the arms rotating with the character tho

pliant cliff
#

one sec lemme review this stuff

#

You might also want to lock the y rotation of your Rigidbody here:

#

we don't want the physics engine rotating fighting with us

#

Since we're manually rotating in our script

modern isle
pliant cliff
#

yes

modern isle
#

Ok so the thing is weird now

#

When I rotate the mouse the character rotates

#

but the arms and camera dont

#

even tho they are parented to the character

pliant cliff
#

the only reason that would happen is:

  • Some other script setting their rotation (such as the second copy of the camera rotation script)
  • If they have separate Rigidbodies
#

oh wait

#

I think I see the problem

#

I do see the problem

modern isle
pliant cliff
#

This is the problem:

            ModelPivot.transform.rotation = Quaternion.Euler(-XAngle, 0, 0);
            Model.transform.rotation = Quaternion.Euler(0, YAngle, 0);```
#

Specifically this:

            ModelPivot.transform.rotation = Quaternion.Euler(-XAngle, 0, 0);
modern isle
pliant cliff
#

It needs to be this:

ModelPivot.transform.localRotation = Quaternion.Euler(-XAngle, 0, 0);```
#

rotation -> localRotation

modern isle
#

I didnt know localRotation existed

#

makes a lot of sense

modern isle
#

Tho you said the gravity was a problem?

pliant cliff
#

the way you're doing gravity is a slight problem yeah

modern isle
#

Could you explain why?

modern isle
#

How could I do it better?

pliant cliff
#

Correct, you need to fix it, not remove it

pliant cliff
modern isle
#

I do notice the character going up slopes is slower than walking

#

the gravity does feel too slow

pliant cliff
#

you should really do this:

    ```cs

if (Character != null)
{
Vector2 Direction = MovementV2.ReadValue<Vector2>();
Rigidbody Rigidbody = Character.GetComponent<Rigidbody>();
float yVel = Rigidbody.linearVelocity.y;

        Vector3 Z = Direction.x * Character.transform.right;
        Vector3 X = Direction.y * Character.transform.forward;

        Vector3 ForceDirection = (Z + X) * MovementSpeed;
        ForceDirection.y = yVel;

        Debug.Log($"Direction: {Direction}, ForceDirection: {ForceDirection}, Forward: {Character.transform.forward}, Right: {Character.transform.right}");

        Rigidbody.linearVelocity = ForceDirection;

    }```
modern isle
#

And the character can walk up stairs faster

pliant cliff
#

yeah you were just instantly setting their y velocity to -9.8 at all times no matter what before