- Home /
Question by
elvish_unity · Jan 11, 2019 at 07:59 PM ·
movementphysicsplayershake
Shaking/Jitter when walking backwards, facing front.
I have a PlayerScript which enables my Player to walk, but when I walk backwards, and facing camera front, it kinda shakes a little bit, but not if I walk backwards diagonally.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScripts : MonoBehaviour
{
public float health;
public float moveSpeed;
public float jumpForce;
public float gravityScale;
public GameObject playerModel;
public Animator anim;
public Transform pivot;
public float rotateSpeed;
private Vector3 moveDirection;
public CharacterController controller;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
anim.SetTrigger("mouseButton");
}
if(health <= 0)
{
anim.SetBool("death", true);
}
float yStore = moveDirection.y;
// moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, Input.GetAxis("Vertical") * moveSpeed);
moveDirection = (transform.forward * Input.GetAxisRaw("Vertical")) + (transform.right * Input.GetAxisRaw("Horizontal"));
moveDirection = moveDirection.normalized * moveSpeed;
moveDirection.y = yStore;
if (controller.isGrounded)
{
moveDirection.y = 0f;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
{
transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
}
anim.SetFloat("speed", (Mathf.Abs(Input.GetAxisRaw("Vertical")) + Mathf.Abs(Input.GetAxisRaw("Horizontal"))));
}
}
Comment
Your answer
Follow this Question
Related Questions
Player object falling through game environment, collision not detected with ground 0 Answers
Sphere get stuck in floor and fails to jump? 1 Answer
Player is Speeding up in collisions 0 Answers
Which is better for player movement: physics or translation 1 Answer
Rigid body child Player movement 0 Answers