Question by
ISAKUPOLY · Nov 12, 2020 at 11:56 PM ·
charactercontrollercamera-movementcharacter movement
Making the character move where the camera is looking
I have a nice working camera script and a playermovement script but I want the character's forward to be relative to my camera...
here is my character script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BiboxController : MonoBehaviour
{
public float moveSpeed;
public float rotationSpeed;
private Vector3 movementVector;
private Rigidbody rBody;
private Animator anim;
private void Awake()
{
rBody = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
}
private void Update()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
movementVector = new Vector3(h, 0, v);
if (movementVector != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movementVector), rotationSpeed);
}
Move();
}
private void Move()
{
movementVector = movementVector.normalized * moveSpeed * Time.deltaTime;
rBody.MovePosition(transform.position + movementVector);
bool IsRunning = movementVector.x != 0f || movementVector.z != 0f;
anim.SetBool("IsRunning", IsRunning);
}
}
And here is my camerascript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCameraController : MonoBehaviour {
public float RotationSpeed = 3;
public Transform Target, Player, cam;
float mouseX, mouseY;
void Start ()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
void LateUpdate()
{
CamControll();
}
void CamControll()
{
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);
Player.rotation = Quaternion.Euler(mouseY, mouseX, 0);
}
}
Easy solutions would be very nice since I'm not really much experienced in this ^^'
Comment
Your answer
Follow this Question
Related Questions
Error CS0103 0 Answers
CharacterController bounce on ramp instead of smooth walk 0 Answers
IsGrounded flickers when hitting a wall 0 Answers