#Is this how movement in a game is supposed to work?
1 messages · Page 1 of 1 (latest)
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 😭 )?
Is your crosshair actually centered?
Also the gravity part doesnt seem right
But thats unrelated
Unless your gravity has non-zero X or Z values
I think it is, let me check
How else could I add gravity ?The script that adds gravity is at the top
yeah it looks centered
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?
could be lots of things.
Your input might include a little horizontal.
Your camera might be slightly rotated relative the the character object
how could the input be horizontal tho?Its just wasd
Lots of reasons, the simplest being maybe you have a gamepad plugged in you forgot about.
HAve you ever heard of Debug.Log?
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
A quick check on debug log
Ok debug prints that it goes a little to the right
Is this because im doing Z+X?
what did you print specifically?
The direction in which the player is moving
you should print both of those variables separately
ForceDirection and Direction
and label them
and let us know what you see
ok so when I debug Direction it doesnt show error when I print ForceDirection it shows that it goes a little to the right
This is because im doing X+Z I think
can you show the actual code you wrote
and the console window with the logs
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
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
thanks that cool , I didnt know debug could do this
(this is what i meant by labels)
Debug.Log just prints any string you give it
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);```
ok so - the last part of the log is showing us pretty clearly your character is just rotated slightly
I only moved forward
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
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?
They arent rotated
I did make a bridge to test it better
and it goes to the right
let me test something
I removed the side movement and I got this
the forward movement it bad I think
right now
yes I understand that - can you show the code of the scripts?
All of it?
📃 Large Code Blocks
Use links to services like:
https://paste.mod.gg/, https://hastebin.skyra.pw/, https://paste.ofcode.org/, https://paste.myst.rs/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
```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
needs to be like:
```cs
code here
```
```cs
code
but it's fine
```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 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
```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;
}
}
}
Its slow tho
But its a update?
after you remove Time.deltaTime it will feel very fast
so you will need to reduce turningspeed
mouse delta is already framerate independent
it shouldn't be multiplied by deltaTime
A thanks
it's already "how much did the mouse move this frame"
I understood how delta time works now
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)
wait
is this a second copy of the camera rotation script?
That could be the issue
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
see if removing the second one changes anything
Increase the TurnSpeed in the inspector
(for the remaining camera rotation script)
no its not that, the screen glitched and it doesnt turn
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
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
the y rotation too?
yes
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
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
If none of those are true(I deleted the second camera rotation script and Only the character has a rigid body) then im kind of meh
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);
I doubted that part too
It needs to be this:
ModelPivot.transform.localRotation = Quaternion.Euler(-XAngle, 0, 0);```
rotation -> localRotation
It works now thanks
Tho you said the gravity was a problem?
the way you're doing gravity is a slight problem yeah
Could you explain why?
If I remove it, the character wont fall
How could I do it better?
Correct, you need to fix it, not remove it
Do you notice right now when the player falls, they aren't falling naturally? They are instantly going to full speed and not accelerating ever right?
I do notice the character going up slopes is slower than walking
the gravity does feel too slow
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;
}```
Gravity feels better now thanks
And the character can walk up stairs faster
yeah you were just instantly setting their y velocity to -9.8 at all times no matter what before