I’m trying to make my character move forwards in the way that the camera is facing, but it doesn’t seem to work. I’ve tried multiple solutions in which none have worked. it still moves on the world space.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public Transform Player;
public Transform Head;
public Rigidbody rb;
public Camera camera;
private float speed = 20.0f;
private float jumpforce = 1.0f;
public bool CanJump = true;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetKey(KeyCode.D))
{
transform.position += transform.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
transform.position -= transform.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.Space))
{
if (CanJump == true)
{
rb.velocity = new Vector3(0, 5, 0);
}
}
if (Input.GetKey(KeyCode.W))
{
transform.position += transform.forward * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.S))
{
transform.position -= transform.forward * speed * Time.deltaTime;
}
}
void OnCollisionEnter(Collision collision)
{
CanJump = true;
}
void OnCollisionExit(Collision collision)
{
CanJump = false;
}
}