How can I make the character turn to face the camera?
So I'm using a camera and player controller. The camera is a free-moving camera, i.e it spins around the player, rather than having the character turn with it, like many 3rd person games these days. However the character will only move the way the player is facing. What I want is for the character to rotate and come towrds the camera. Does that sound complicated? (Imagine Zelda BOTW, turn the camera to look at Link's face, pull down on the stick, or s on a keyboard, and he runs towards the camera. My player, turn camera to look at face, press s, moves backwards. I'll send the code)
CAMERA
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ThirdPersonCameraController : MonoBehaviour { public float RotationSpeed; public Transform Target, Player; float mouseX, mouseY;
void Start()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
void LateUpdate()
{
CamControl();
}
void CamControl()
{
mouseX += Input.GetAxis("Mouse X") * RotationSpeed;
mouseY -= Input.GetAxis("Mouse Y") * RotationSpeed;
mouseY = Mathf.Clamp(mouseY, -35, 60);
transform.LookAt(Target);
Target.rotation = Quaternion.Euler(mouseY, mouseX, 0);
}
}
CHARACTER CONTROLLER
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ThirdPersonCharacterController : MonoBehaviour { public float speed;
void Update()
{
PlayerMovement();
}
void PlayerMovement()
{
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
Vector3 playerMovement = new Vector3(hor, 0f, ver) * speed * Time.deltaTime;
transform.Translate(playerMovement, Space.Self);
}
}
Your answer
Follow this Question
Related Questions
Bifocal Depth of Field 0 Answers
Animation not working when Camera is attached to script 0 Answers
Need help with a 3rd person Camera 0 Answers
Camera movement with arrow keys? 1 Answer
Computer screen inside of game 1 Answer