Rigidbody rb;
Vector3 currMovement;
public float jumpSpeed = 10;
public float moveSpeed = 10;
public float rotSpeed = 180;
float distToGround;
public float smoothTimePos = 0.25f;
float currPos;
float targetPos;
float posVel;
public float smoothTimeRot = 0.25f;
float currRot;
float targetRot;
float rotVel;
void Start()
{
rb = GetComponent<Rigidbody>();
distToGround = GetComponent<Collider>().bounds.extents.y;
}
bool isGrounded() { return Physics.Raycast(transform.position, Vector3.down, distToGround + 0.1f); }
void Update()
{
MovetargetRot = Input.GetAxisRaw("Horizontal"); * rotSpeed * Time.smoothDeltaTime;
} targetPos = Input.GetAxisRaw("Vertical") * moveSpeed;
void Move if (isGrounded())
{
// Rotation smoothing if (Input.GetButtonDown("Jump"))
targetRot = Input rb.GetAxisRaw("Horizontal")velocity *+= rotSpeedVector3.up * Time.smoothDeltaTime;jumpSpeed;
}
// Rotation smoothing.
if (targetRot > 360)
targetRot -= 360;
if (targetRot < 0)
targetRot += 360;
currRot = Mathf.SmoothDampAngle(currRot, targetRot, ref rotVel, smoothTimeRot);
transform.eulerAngles += new Vector3(0, currRot, 0);
// Movement smoothing.
targetPos = Input.GetAxisRaw("Vertical") * moveSpeed;
currPos = Mathf.SmoothDamp(currPos, targetPos, ref posVel, smoothTimePos);
currMovement = new Vector3(0, 0, currPos);
currMovement = transform.rotation * currMovement;
}
if
void (isGroundedFixedUpdate())
{
if (Input.GetButtonDownMove("Jump"));
rb.velocity += Vector3.up * jumpSpeed;}
void }Move()
{
rbtransform.position += currMovement * Time.smoothDeltaTime;
}
public TransformRigidbody player;
Quaternion targetLook;
Vector3 targetMove, targetMoveRaycast, targetMoveUse;
public float smoothLook = 8, smoothMove = 0.25f;
public float distFromPlayer = 5, heightFromPlayer = 3;
Vector3 moveVel;
void LateUpdate()
{
CameraMove();
}
void CameraMove()
{
targetMove = player.position + (player.rotation * new Vector3(0, heightFromPlayer, -distFromPlayer));
RaycastHit hit;
if (Physics.Raycast(player.position, targetMove - player.position, out hit, Vector3.Distance(player.position, transform.position)))
{
targetMoveRaycast = hit.point;
targetMoveUse = targetMoveRaycast;
}
else
{
targetMoveUse = targetMove;
}
transform.position = Vector3.SmoothDamp(transform.position, targetMoveUse, ref moveVel, smoothMove); // This line of code is making the camera shake.
//transform.position = Vector3.Slerp(transform.position, targetMoveUse, smoothMove); // But this line of code just works perfect for movement, but I want to use a SmoothDamp, without camera shaking.
// If I use the line with the Slerp then my Raycasts for camera doesn't work properly, so I need a solution for my SmoothDamp to work properly without camera shake.
targetLook = Quaternion.LookRotation(player.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetLook, smoothLook * Time.smoothDeltaTime);
}