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 */
}
}