Search Unity

While camera follow character side(angle) camera "jump"

Discussion in 'Scripting' started by owczarekniemiecki5, Dec 15, 2019.

  1. owczarekniemiecki5

    owczarekniemiecki5

    Joined:
    Apr 12, 2019
    Posts:
    4
    In this video is shown:

    This only happens when moving and rotate at the same time.Camera is a child of a rabbit.I tried this script:
    [SerializeField]
    private Transform target;

    [SerializeField]
    private Vector3 offsetPosition;

    [SerializeField]
    private Space offsetPositionSpace = Space.Self;

    [SerializeField]
    private bool lookAt = true;

    private void LateUpdate()
    {
    Refresh();
    }

    public void Refresh()
    {
    if (target == null)
    {
    Debug.LogWarning("Missing target ref !", this);

    return;
    }

    // compute position
    if (offsetPositionSpace == Space.Self)
    {
    transform.position = target.TransformPoint(offsetPosition);
    }
    else
    {
    transform.position = target.position + offsetPosition;
    }

    // compute rotation
    if (lookAt)
    {
    transform.LookAt(target);
    }
    else
    {
    transform.rotation = target.rotation;
    }
    }
    But it happens the same.

    The rabbit script looks like this:

    private Animator anim;
    private Rigidbody rb;
    private float vertical;
    private float horizontal;

    void Start()
    {
    anim = GetComponent<Animator>();
    rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
    vertical = Input.GetAxis("Vertical");
    horizontal = Input.GetAxis("Horizontal");
    //horizontal = Input.GetAxis("Horizontal");

    //MOVE
    if (vertical>0 && Input.GetKey(KeyCode.LeftShift)) //run
    {
    anim.SetBool("walkback", false);
    anim.SetBool("walk", false);
    //---------------------------
    anim.SetBool("run", true);

    rb.velocity = transform.forward * 300 * Time.deltaTime + new Vector3(0, rb.velocity.y, 0);

    }
    else if(vertical > 0) //walk
    {
    anim.SetBool("walkback", false);
    anim.SetBool("run", false);
    //--------------------------
    anim.SetBool("walk", true);

    rb.velocity = transform.forward * 150 * Time.deltaTime + new Vector3(0, rb.velocity.y, 0);

    }
    else if(vertical<0)//walk back
    {
    anim.SetBool("run", false);
    anim.SetBool("walk", false);
    //---------------------------------
    anim.SetBool("walkback", true);
    rb.velocity = transform.forward * -150 * Time.deltaTime + new Vector3(0, rb.velocity.y, 0);
    }
    else if(vertical==0)//idle
    {
    anim.SetBool("run", false);
    anim.SetBool("walk", false);
    anim.SetBool("walkback", false);
    rb.velocity = new Vector3(0, rb.velocity.y, 0);
    }

    //ROTATE
    if(horizontal>0)
    {
    transform.Rotate(new Vector3(0, 50, 0) * Time.deltaTime);
    }
    else if(horizontal<0)
    {
    transform.Rotate(new Vector3(0, -50, 0) * Time.deltaTime);
    }
    }
     
  2. owczarekniemiecki5

    owczarekniemiecki5

    Joined:
    Apr 12, 2019
    Posts:
    4