#what should i do with forward movement

1 messages · Page 1 of 1 (latest)

mental grail
#

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

gentle crown
#

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);
    }
}

mental grail
#

no, I'm not writing your code for you

gentle crown
#

since i have honestly no idea

mental grail
#

we're going to figure it out

gentle crown
#

point out the lines

mental grail
gentle crown
#

No

#

Wait

#

yes

#

I do

mental grail
#

it's a constant

gentle crown
#

Yes

mental grail
#

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

gentle crown
#

i understand a bit more

mental grail
#

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

gentle crown
#

yes

mental grail
#

but, from the outside, 'forwards' depends on which way you are facing

#

that's the distinction

gentle crown
#

i understand

#

so how should i fit it into the script?>

mental grail
#

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 * [...];

gentle crown
#

hold on

#

uhh

#

transform.right?

mental grail
#

indeed, for the first blank

gentle crown
#

and transform.forward

mental grail
#

that is a vector pointing to your right

#

indeed

#

combining those two produces a new vector

gentle crown
#

Yes

#

i understand now

#

but where would i actually fit it into the script

#

i am sorry

mental grail
#

well, what do you want to do with this vector?

gentle crown
#

literally just started my first 3d project

mental grail
#

it tells you the direction you want to go

gentle crown
#

multiply it?

mental grail
#

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

gentle crown
#

velocity

mental grail
#

right, the line that you set the rigidbody's velocity

#

so, instead of setting it to that incorrect vector...

gentle crown
#

still dont know

#

i am lost

#

clueless

mental grail
#

what does setting rb.velocity do?

gentle crown
#

make the player move

mental grail
#

indeed

mental grail
#
Vector3 moveVec = x * transform.right + y * transform.forward;
#

(replacing x and y with whatever names you used for horizontal and vertical input)

gentle crown
#

ok

mental grail
#

...you now know which direction you want to move in

gentle crown
#

like this?

mental grail
#

correct

gentle crown
#

how would i use my speed

mental grail
#

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

gentle crown
#

now my camera refuses to go

mental grail
#

if your y velocity is 3.6, then this will add [0, 3.6, 0]

gentle crown
#

*move

mental grail
#

show your code

gentle crown
#
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);
    }
}
mental grail
#

looks like you aren't rotating around the vertical axis

gentle crown
#

in the line player.Rotate(Vector3.up * inputX);

mental grail
#

yeah, that seems reasonable

gentle crown
#

its rotating the player

#

but it just wont

mental grail
#

is the camera parented to the player?

gentle crown
#

Yers

#

Yes

#

So what should i do

#

@mental grail

mental grail
#

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?

gentle crown
#

It is not

#

the camera is

mental grail
#

the Y axis is the vertical axis, and it's the one the player should be rotating around.

gentle crown
#

nope

#

both on 0

mental grail
#

oh

#

i just remembered

#

you have a rigidbody on the player

gentle crown
#

yes

mental grail
#

you should not directly mess with the Transform of something with a Rigidbody on it

#

instead, you can ask the rigidbody to rotate

gentle crown
#

so just replace Player with rb?

mental grail
#

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

gentle crown
#

i am not following

#

what is "angletoRotateBy"

mental grail
#

a float

#

Quaternion.AngleAxis(45f, Vector3.up)

#

this produces a 45 degree rotation around the Y axis

#

you already compute this