- Home /
Other
Will someone please help me???
I've asked multiple questions, an no-one is helping. I feel like I'm not asking to much and its more of a problem than a question... So here it goes..
I am using the new input system and cinamachine. Okay so, my players direction automatically updates with the rotation of the camera, but for some reason the players rotation doesn't actually update.. is there a solution to this if so could someone please help.
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerControrls : MonoBehaviour
{
[SerializeField]
InputActionReference playerMovementControls;
//...//
[SerializeField]
float movementSpeed;
Vector2 movementDirections;
//...//
[SerializeField]
float rotationSpeed;
Quaternion rotate;
//...//
[SerializeField]
InputActionReference jumpControl;
//...//
[SerializeField]
float jumpVelocity;
[SerializeField]
float fallMultiplier;
//...//
[SerializeField]
float gravityScale;
[SerializeField]
float globalGravity;
//...//
[SerializeField]
bool isGrounded;
//...//
Rigidbody playerRigidbody;
private Transform cameraMainTransform;
private void OnEnable()
{
playerMovementControls.action.Enable();
jumpControl.action.Enable();
}
//-----//
private void OnDisable()
{
playerMovementControls.action.Disable();
jumpControl.action.Disable();
}
//-----//
void Awake()
{
playerRigidbody = GetComponent<Rigidbody>();
cameraMainTransform = Camera.main.transform;
}
void Start()
{
playerRigidbody.useGravity = false;
}
//-----//
void FixedUpdate()
{
Move();
Jump();
}
//-----//
void Move()
{
movementDirections = playerMovementControls.action.ReadValue<Vector2>();
var movement = new Vector3(movementDirections.x, 0, movementDirections.y);
Vector3 playerMoving = movement * movementSpeed;
playerMoving = cameraMainTransform.forward * playerMoving.z
+ cameraMainTransform.right * playerMoving.x;
playerMoving.y = playerRigidbody.velocity.y;
playerRigidbody.velocity = playerMoving;
if (movementDirections != Vector2.zero)
{
rotate = Quaternion.LookRotation(movement);
rotate = Quaternion.RotateTowards(playerRigidbody.rotation,
rotate,
rotationSpeed * Time.fixedDeltaTime);
playerRigidbody.MoveRotation(rotate);
}
}
//-----//
void Jump()
{
if (jumpControl.action.triggered && isGrounded)
{
playerRigidbody.AddForce(Vector3.up * jumpVelocity,ForceMode.Impulse);
isGrounded = false;
}
Vector3 gravity = globalGravity * gravityScale * Vector3.up;
if (playerRigidbody.velocity.y < 0)
{
playerRigidbody.AddForce(gravity * fallMultiplier, ForceMode.Acceleration);
}
else
{
playerRigidbody.AddForce(gravity, ForceMode.Acceleration);
}
}
//-----//
void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.layer == LayerMask.NameToLayer("Ground"))
isGrounded = true;
}
}
At first glance, it seems maybe Move should be in Update, not FixedUpdate.
Other than that, I'm not too familiar with Cinemachine or the new Input system, but in lieu of an actual answer, I'd say put a load of Debug.Log() along the process for the player's camera movement. see which parts are running and which parts aren't that should be, May be an idea to make them output the player's rotation.eulerAngles along with "rotate" to the Console, so you can check if the game is setting those variables correctly.
Thats likely why you haven't got an answer yet. You've posted a 110 line long script and said it isn't working, but without knowing how your project is set up or where exactly the script is going wrong, it's hard for anyone to help unless they happen to know alot about Cinemachine and the Input system.
this is the part I need help with
void Move()
{
movementDirections = playerMovementControls.action.ReadValue<Vector2>();
var movement = new Vector3(movementDirections.x, 0, movementDirections.y);
Vector3 playerMoving = movement * movementSpeed;
playerMoving = cameraMainTransform.forward * playerMoving.z + cameraMainTransform.right * playerMoving.x;
playerMoving.y = playerRigidbody.velocity.y;
playerRigidbody.velocity = playerMoving;
if (movementDirections != Vector2.zero)
{
rotate = Quaternion.LookRotation(movement);
rotate = Quaternion.RotateTowards(playerRigidbody.rotation, rotate, rotationSpeed * Time.fixedDeltaTime);
playerRigidbody.MoveRotation(rotate);
}
}
Also, the input system isnt really involved here, i just need the player to rotate with the cameras rotation.
Answer by ZadiusC · Jul 22, 2021 at 07:37 PM
Try deleting the 'look at' object. Then it will just follow the player and look wherever the player is looking, it won't be trying to look at the player
That doesn't work sadly. I just tried my characters rotation still points in the direction it was originally going even though its moving in a different direction
What happens if you disable the script you have on your player (just unclick the arrow beside it) then drag the player camera onto the player (or use the add component button)? Test
See how you have a red icon beside the main camera, there should be another beside the second camera or the player (can't recall). Trying to find the video I used to learn it, I'll post it if I can find it.
Edit-- Here's a link to the cinemachine tutorial on unity. Great way to learn the in's and out's in an hour https://learn.unity.com/tutorial/cinemachine#5c7f8528edbc2a002053b4ea
Hey, so I changed
rotate = Quaternion.LookRotation(movement,Vector3.up);
too
rotate = Quaternion.LookRotation(playerMoving,Vector3.up);
now my player rotates like i want, but now my y axis on its position is wanting to change
Answer by PlayZzOfficial · Jul 22, 2021 at 05:45 PM
Here is a video showing the issues, don't mind the position values, look at the Rotation value for the Y it wont update when changing the camera direction. here is a video https://drive.google.com/file/d/1jpklOaD5ePBQ0rarJiQNsMO02Q8-eC5n/view?usp=sharing
Ok, so it is rotating, but not the way you want it to. I have a feeling the answer probably lies in lines 80-85. Perhaps try doing a Debug.Log that Outputs "rotate.eulerAngles" right before you call "playerRigidbody.MoveRotation(rotate)". See if rotate is actually getting set to the correct value to begin with.
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
How to clamp X axis rotation of a GameObject with a Rigidbody attached. 0 Answers
How to limit rigidbody rotation to one axis at a time? 1 Answer
Need help rotating child of player based off of player movement. 1 Answer
resetting rotation on a single axis 2 Answers