- Home /
How to make character move in the direction its facing
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;
}
}
Answer by Klarzahs · Oct 23, 2020 at 11:07 PM
Hi @LyraxH,
I assume the script is attached to the player object? Then transform.forward (or any of the transform.X) is using the players transform. If you want to get the looking direction of the camera, use Camera.main.transform.forward.
Note: I think querying Camera.main was a bit performance heavy, so save it in a variable
Hi @Klarzahs
Thanks for the response, but I already have a script to make the body follow the camera position here: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Looking : $$anonymous$$onoBehaviour
{
public GameObject Head;
float yaw = 0f;
float pitch = 0f;
public float lookspeed = 2.5f;
public float maxY = -65;
public float $$anonymous$$Y = 50;
void Start()
{
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
Cursor.visible = false;
}
void Update()
{
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
yaw += lookspeed * Input.GetAxis("$$anonymous$$ouse X");
pitch -= lookspeed * Input.GetAxis("$$anonymous$$ouse Y");
pitch = $$anonymous$$athf.Clamp(pitch, maxY, $$anonymous$$Y);
}
}
but it still uses world space for some reason. Thanks
Are both of the scripts attached to the player? Then it should work, I copy/pasted it into a blank solution and it worked (even though your view rotation is method.. weird). If both are attached to the player, can you upload your $$anonymous$$imized project to github/dropbox etc, I'll have a look at it
Oh I got it to work, I just didn't have both scripts attached to the player. Thanks!
Your answer
Follow this Question
Related Questions
How to Move and Rotate Ball at the same time by code 0 Answers
any way to access the global position and rotation of a child object? 1 Answer
Vertical Camera Orbit 0 Answers
How to get updated Transform of a spawned object? 1 Answer
Possible way to improve performance moving large hierarchies? 1 Answer