- Home /
Connection between CinemaMashine - Player Rotation
Hello Guys,
I have problem with connection between CinemaMashine - Player Rotation. In play mode whole screen jumping i rotate very fast. What is interesting if i move cursor to border of my screen 'jumping effect of screen' is less visible.
In the end i would like to achieve : 1)when i move cursor character (Player) will rotate (without camera) 2) when i hold left button of mouse character will rotate with camera
This is my code :
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed = 5f;
public float jumpForce = 12f;
public float gravityScale = 5f;
private Vector3 moveDirection;
public CharacterController charControler;
public Animator anim;
public CapsuleCollider _playerCollider;
Vector3 lookPos;
void Start()
{
}
void Update()
{
float yStore = moveDirection.y;
moveDirection = (transform.forward * Input.GetAxisRaw("Vertical")) + (transform.right * Input.GetAxisRaw("Horizontal"));
moveDirection.Normalize();
moveDirection = moveDirection * moveSpeed;
moveDirection.y = yStore;
if (isGrounded() && Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
else if (charControler.isGrounded)
{
moveDirection.y = 0f;
}
moveDirection.y += Physics.gravity.y * Time.deltaTime * gravityScale;
charControler.Move(moveDirection * Time.deltaTime);
anim.SetFloat("Speed", Mathf.Abs(moveDirection.x) + Mathf.Abs(moveDirection.z));
//anim.SetBool("Grounded", charControler.isGrounded);
}
private bool isGrounded()
{
return Physics.Raycast(transform.position, -Vector3.up, _playerCollider.bounds.extents.y + 0.1f);
}
void FixedUpdate()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
lookPos = hit.point;
}
Vector3 lookDir = lookPos - transform.position;
lookDir.y = 0;
transform.LookAt(transform.position + lookDir, Vector3.up);
}
}
Your answer
Follow this Question
Related Questions
3rd Person Orbit Camera in LOCAL Space 0 Answers
Is it possible to reuse input's acceleration in custom script? 2 Answers
Help with a 2d camera Controller 1 Answer
Why is Camera turning different than Player with same code in different scripts? 1 Answer
Previously functioning camera follow script stutters in new project 1 Answer