#what should i do with forward movement
1 messages · Page 1 of 1 (latest)
no, that's what you were doing, roughly.
transform.forward * verticalInput is a vector that points in the direction you want to move forwards or backwards
Vector3.forward * verticalInput is a vector that points in the +Z or -Z direction, no matter what your orientation is
you were doing the latter
this is wrong: it doesn't depend on your rotation
suggest edits in this script for me
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementScript : MonoBehaviour
{
private float horizontal;
private float vertical;
public float speed = 5;
private Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
rb.velocity = new Vector3(horizontal * speed, rb.velocity.y, vertical * speed);
}
}
no, I'm not writing your code for you
since i have honestly no idea
we're going to figure it out
point out the lines
first: do you understand why this is wrong?
it's a constant
Yes
so it can't possibly be the correct world-space forward direction
i.e. the direction you're facing if you're just looking from the outside
i understand a bit more
Vector3.forward is the correct forward direction...if you think about it in the local space of the object
if I ask you to point forwards, you always make the same movement
you raise your arm and point straight ahead
so, in your perspective, 'forwards' is a constant
yes
but, from the outside, 'forwards' depends on which way you are facing
that's the distinction
you were calculating the direction to move in local space, but you then used it to create a velocity that's in world space
one solution is to compute the direction to move in world space from the start
new Vector3(x, 0, z) is the same as writing x * Vector3.right + z * Vector3.forward
so, if Vector3.right and Vector3.forward need to be replaced, then, what do you think a more correct line would be?
Vector3 moveVec = x * [...] + z * [...];
indeed, for the first blank
and transform.forward
that is a vector pointing to your right
indeed
combining those two produces a new vector
Yes
i understand now
but where would i actually fit it into the script
i am sorry
well, what do you want to do with this vector?
literally just started my first 3d project
it tells you the direction you want to go
multiply it?
will that make your character move?
probably not.
what does your script currently do that makes the character move?
it's one very specific line
velocity
right, the line that you set the rigidbody's velocity
so, instead of setting it to that incorrect vector...
what does setting rb.velocity do?
make the player move
indeed
and, having written...
Vector3 moveVec = x * transform.right + y * transform.forward;
(replacing x and y with whatever names you used for horizontal and vertical input)
ok
...you now know which direction you want to move in
correct
how would i use my speed
you compute the direction you want to go, and then tell the player to move like that
multiply moveVec by it before setting the velocity
also, you'll probably want to keep your current vertical velocity
something like moveVec += rb.velocity.y * Vector3.up;
get the y velocity, multiply by that by up vector, add it
now my camera refuses to go
if your y velocity is 3.6, then this will add [0, 3.6, 0]
*move
show your code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementScript : MonoBehaviour
{
private float horizontal;
private float vertical;
public float speed = 5;
private Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
Vector3 moveVec = horizontal * transform.right + vertical * transform.forward;
rb.velocity = moveVec;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRotate : MonoBehaviour
{
public Transform player;
public float mouseSensitivity = 2f;
float cameraVerticalRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
float inputX = Input.GetAxis("Mouse X") * mouseSensitivity;
float inputY = Input.GetAxis("Mouse Y") * mouseSensitivity;
cameraVerticalRotation -= inputY;
cameraVerticalRotation = Mathf.Clamp(cameraVerticalRotation, -90f, 90f);
transform.localEulerAngles = Vector3.right * cameraVerticalRotation;
player.Rotate(Vector3.up * inputX);
}
}
looks like you aren't rotating around the vertical axis
in the line player.Rotate(Vector3.up * inputX);
yeah, that seems reasonable
is the camera parented to the player?
how do you know the player is being rotated?
did you look in the inspector and see that the player is rotating around the Y axis?
the Y axis is the vertical axis, and it's the one the player should be rotating around.
yes
you should not directly mess with the Transform of something with a Rigidbody on it
instead, you can ask the rigidbody to rotate
so just replace Player with rb?
you give it a quaternion
so, you should do something like this
Quaternion deltaRotation = Quaternion.AngleAxis(angleToRotateBy, Vector3.up);
rb.MoveRotation(rb.rotation * deltaRotation);
a Quaternion represents a rotation
multiplying two Quaternions combines the rotations
so, if deltaRotation is a very small rotation around the Y axis
this will slightly turn the player around the Y axis
Quaternion.AngleAxis rotates you by a specified number of degrees around an axis