2
\$\begingroup\$

Here is my player code.

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()
{
    targetRot = Input.GetAxisRaw("Horizontal") * rotSpeed * Time.smoothDeltaTime;
    targetPos = Input.GetAxisRaw("Vertical") * moveSpeed;
}

void FixedUpdate()
{
    Move();
}

void Move()
{
    if (isGrounded())
    {
        if (Input.GetButtonDown("Jump"))
            rb.velocity += Vector3.up * 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.
    currPos = Mathf.SmoothDamp(currPos, targetPos, ref posVel, smoothTimePos);
    currMovement = new Vector3(0, 0, currPos);
    currMovement = transform.rotation * currMovement;

    rb.position += currMovement * Time.smoothDeltaTime;
}

I have a Rigidbody attached to my player. I think the problem is with my camera script. Here is my camera script.

public Rigidbody 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);

    targetLook = Quaternion.LookRotation(player.position - transform.position);
    transform.rotation = Quaternion.Slerp(transform.rotation, targetLook, smoothLook * Time.smoothDeltaTime);
}

The player is not an parent of my camera. When I parent the player to my camera the shake stops. But I want a custom smooth camera movement with my custom scirpt, so I can't make the player a parent of the camera.

\$\endgroup\$

marked as duplicate by MichaelHouse Jun 13 '16 at 15:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2
\$\begingroup\$

Try this:

  1. Add a kinematic Rigidbody to the camera
  2. Cache the rigidbody
  3. Use rigidbody.position and rigidbody.rotation instead of transform.position and transform.rotation
  4. Call CameraMove() from FixedUpdate() instead of LateUpdate()
  5. Play around with interpolation on the camera and the player if necessary

    public Rigidbody player;
    
    Quaternion targetLook;
    Vector3 targetMove, targetMoveRaycast, targetMoveUse;
    public float smoothLook = 8, smoothMove = 0.25f;
    public float distFromPlayer = 5, heightFromPlayer = 3;
    Vector3 moveVel;
    
    private Rigidbody m_Rigidbody; // added
    
    /// added
    void Awake() {
        m_Rigidbody = GetComponent<Rigidbody>();
        m_Rigidbody.isKinematic = true;
    }
    
    void FixedUpdate() { //Edited
        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, m_Rigidbody.position))) {//Edited
            targetMoveRaycast = hit.point;
            targetMoveUse = targetMoveRaycast;
        }
        else {
            targetMoveUse = targetMove;
        }
    
        m_Rigidbody.position = Vector3.SmoothDamp(m_Rigidbody.position, targetMoveUse, ref moveVel, smoothMove); //Edited
    
        targetLook = Quaternion.LookRotation(player.position - m_Rigidbody.position); //Edited
        m_Rigidbody.rotation = Quaternion.Slerp(m_Rigidbody.rotation, targetLook, smoothLook * Time.smoothDeltaTime); //Edited
    }
    
\$\endgroup\$
  • 1
    \$\begingroup\$ Eyyyy body you helped me SO MUCH, BIG respect to you dude. \$\endgroup\$ – Hayk Avagyan Jun 13 '16 at 18:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.