Player movement

i made a script based off a youtube video, and it works perfectly-i can move forwardss smoothly and sideways too, but it doesntt let my player move continusly without you pushing a button( it does move, but you have to press a button for it to move in a certain direction) I want it to move in the direction my camera is facing continsly
Player movement script

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

public Transform MainCamera;
float heading = 0;
Vector2 input;

public Rigidbody rb; /* Rigid body is a component of unity’s physic system
so i named it rb*/
public float forwardForce = 2000f;
/*Puts the 2000 in the rb.AddForce(ln 21) code sentace and
makes it into a variable So it’s changeable within unity. */
public float sideWaysForce = 150f;
/*Puts the 150 in the if (Input.GetKey rb.AddForce(ln 33 and 45)
code sentace and makes it into a variable So it’s changeable
within unity. */
public float forbackForce = 150f;
/*Puts the 150 in the if (Input.GetKey rb.AddForce(ln 57 and 69)
code sentace and makes it into a variable So it’s changeable
within unity. */

void Update()
{
heading += Input.GetAxis(“Mouse X”) * Time.deltaTime * 180;
MainCamera.rotation = Quaternion.Euler(0, heading, 0);

input = new Vector2(Input.GetAxis(“Horizontal”), Input.GetAxis(“Vertical”));
input = Vector2.ClampMagnitude(input, 5);

Vector3 MainCameraF = MainCamera.forward;
Vector3 MainCameraR = MainCamera.right;

MainCameraF.y = 0;
MainCameraR.y = 0;
MainCameraF = MainCameraF.normalized;
MainCameraR = MainCameraR.normalized;

transform.position += (MainCameraF * input.y + MainCameraR * input.x) * Time.deltaTime * 5;
}

void FixedUpdate() /* I used Fixed update
because we’re dealing with unity’s physics system.*/
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime); //(forwardForce = 2000)
}
}

followplayer script

using UnityEngine;

public class FollowPlayer : MonoBehaviour {

public Transform player; /* Calls the Transform compont of the Gameobject
player */

public Vector3 offset; /* ‘Vector3’ includes 3 numbers(x, y,z)
and it’s name is offset */

void Start()
// Start is called before the first frame update
{

}

// Update is called once per frame
void Update()
{
transform.position = player.position + offset;
/* calls the transform component of a linked object and equals that to the
same GameObject ‘z’ position */

}
}

Hi and welcome! Please use code-tags to post code examples.
You can add code by pressing the <> button. There is also a sticky on this subforum explaining it :slight_smile:

As for your question, everything that has a Transform component, directly tells you its forward direction. You can access the main camera with Main.Camera. So you can get its facing direction (Vector3) by writing this:

Vector3 camForward = Camera.main.gameObject.transform.forward;
Vector3 camForwardDelta = camForward * Time.deltaTime;

By multiplying it with Time.deltaTime you get the fraction of the vector that represents the 1/FPS, such that you move exactly “one forward” per second. To make your movement speed faster, multiply it with some movespeed float variable. You can now add that to your position in Update() and will move in the direction of the camera with the desired speed.

However, i’m not sure if that’s actually what you want. What type of game are you making? In most games the camera is angled (or rotatable) so this would result in very weird behavior.

Also, from your comments i see that you are still very new to coding. I can recomment you the free youtube tutorial GameDev series by Sebastian Lague, which goes over everything (basic) you need in terms of coding and Unity to get started. It’s increadibly well made.

Thanks for responding Yoreki

Yes, I am very, very new to game development and coding. Its been about 1 month and 25 days and I am so excited to learn all the ins and outs of game development.

I am creating an arcadey style of game where you have a character and you have to avoid obstacles and collects points so you can buy stuff in the shop. Its gonna be awesome. (and no, my camera isn’t rotated

and thanks for referencing a youtube channel for me to check out.