- Home /
Rotate character with camera ?!
i am trying to modify the character movement script to make it rotate with the camera rotation
the problem i don't success to make it after reading some topic i get really confused about this issue
the code is able to create movement with cam direction but not rotation with cam
i tried to reference the cam "i use cinemachine" and link it to character rotation but this end with wired behavior "shaking move's"
here is the code thank you unity community
public class MovmenetWithCamera : MonoBehaviour
{
Vector3 _CharacterDirection, realativedir;
Quaternion _CharacterRotation;
Animator _CharacterAnim;
Rigidbody _CharacterRigidbody;
[SerializeField]
float TurnSpeed = 8, speed = 6;
bool _IsWalking;
void Start()
{
_CharacterAnim = GetComponent<Animator>();
_CharacterRigidbody = GetComponent<Rigidbody>();
}
void Update()
{
CharacterActions();
}
void FixedUpdate()
{
CharacterMovement();
}
void CharacterMovement()
{
//movement input's
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
_CharacterDirection.Set(h, 0, v);
// convert the character dir to cam dir
realativedir = Camera.main.transform.TransformDirection(_CharacterDirection);
realativedir.y = 0f;
realativedir.Normalize();
//bool movement check
bool _IsHorizontalChange = !Mathf.Approximately(h, 0f);
bool _IsVerticalChange = !Mathf.Approximately(v, 0);
_IsWalking = _IsHorizontalChange || _IsVerticalChange;
_CharacterAnim.SetBool("IsWalking", _IsWalking);
//change character forward to the desired one
Vector3 _DesairdForward = Vector3.RotateTowards(this.transform.forward, realativedir, TurnSpeed * Time.deltaTime, 0);
_CharacterRotation = Quaternion.LookRotation(_DesairdForward);
//RigidBody Direction && Rotation
_CharacterRigidbody.MovePosition(_CharacterRigidbody.position + realativedir * speed * Time.deltaTime);
_CharacterRigidbody.MoveRotation(_CharacterRotation);
}
void CharacterActions()
{
if (Input.GetKey(KeyCode.Space))
{
_CharacterAnim.SetTrigger("Shoot");
Debug.Log("shoot");
}
else if (Input.GetKeyDown(KeyCode.Space) && _IsWalking)
{
_CharacterAnim.SetTrigger("Shoot");
}
}
Answer by lTimesl · Oct 24, 2019 at 07:17 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovmenetWithCamera : MonoBehaviour
{
Vector3 _CharacterDirection, realativedir;
Quaternion _CharacterRotation, rot;
Animator _CharacterAnim;
Rigidbody _CharacterRigidbody;
[SerializeField]
float TurnSpeed = 8, speed = 6;
bool _IsWalking;
[SerializeField]
Camera VCam;
void Start()
{
_CharacterAnim = GetComponent<Animator>();
_CharacterRigidbody = GetComponent<Rigidbody>();
}
void Update()
{
CharacterActions();
}
void FixedUpdate()
{
CharacterMovement();
}
void CharacterMovement()
{
//movement input's
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
_CharacterDirection.Set(h, 0, v);
// convert the character dir to cam dir
realativedir = VCam.transform.TransformDirection(_CharacterDirection);
realativedir.y = 0f;
realativedir.Normalize();
//bool movement check
bool _IsHorizontalChange = !Mathf.Approximately(h, 0f);
bool _IsVerticalChange = !Mathf.Approximately(v, 0);
_IsWalking = _IsHorizontalChange || _IsVerticalChange;
_CharacterAnim.SetBool("IsWalking", _IsWalking);
//change character forward to the desired one
Vector3 _DesairdForward = Vector3.RotateTowards(this.transform.forward, realativedir, TurnSpeed * Time.deltaTime, 0);
//_CharacterRotation = Quaternion.LookRotation(_DesairdForward); //use this for orbaiting camera with no rotation in move
//rotate character with cam
rot = VCam.transform.rotation;
rot.x = 0;
rot.z = 0;
transform.rotation = rot;
//RigidBody Direction && Rotation
_CharacterRigidbody.MovePosition(_CharacterRigidbody.position + realativedir * speed * Time.deltaTime);
//_CharacterRigidbody.MoveRotation(rot);
}
void CharacterActions()
{
if (Input.GetKey(KeyCode.Space))
{
_CharacterAnim.SetTrigger("Shoot");
Debug.Log("shoot");
}
else if (Input.GetKeyDown(KeyCode.Space) && _IsWalking)
{
_CharacterAnim.SetTrigger("Shoot");
}
}
}
Your answer
Follow this Question
Related Questions
How can I rotate my camera when player has rigidbody? 0 Answers
Cross platform camera controlls 0 Answers
change rotation using WheelCollider.steerAngle 0 Answers
Stop camera from moving when parent rotates forward 1 Answer
CAR ROTATING HELP 2 Answers