Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Lookat mousePosition goes bananas

Discussion in 'Cinemachine' started by ngerbens, Jun 13, 2018.

  1. ngerbens

    ngerbens

    Joined:
    Nov 20, 2017
    Posts:
    33
    Hello!

    I'm having problems with a flying object which I follow/track with a vCam.
    When I fly around (with the code below attached), everything works fine, as soon as my mouse hits the outside of the screen the camera starts shaking and spinning out of a sudden. Does anyone know what's the matter? I'm not having this issue without the Cinemachine cam.
    1. Transform myT;
    2. public float movementSpeed;
    3. private Vector3 mousePos;
    4. private Vector3 aim;

    5. void Update(){

    6. mousePos = Input.mousePosition;
    7. mousePos += Camera.main.transform.forward * 100f;
    8. aim = Camera.main.ScreenToWorldPoint(mousePos);
    9. this.transform.LookAt(aim);
    10. myT.position += myT.forward * movementSpeed * Time.deltaTime * Input.GetAxis("Vertical-");

    11. }
     
    Last edited: Jun 19, 2018
  2. ngerbens

    ngerbens

    Joined:
    Nov 20, 2017
    Posts:
    33
    Is there please someone who can help me? This is probably a common problem, since lots of people use pointing direction with a mouse, right?
     
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Can you show me the inspector for your virtual camera?
     
  4. ngerbens

    ngerbens

    Joined:
    Nov 20, 2017
    Posts:
    33
    Yes, here it is:

    Schermafbeelding 2018-06-28 om 23.11.52.png
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Several problems here!

    In your script, You have to use local camera Z for ScreenToWorldPoint, like it says in the docs. Change your line to
    Code (CSharp):
    1. mousePos.z = 100;
    Turn off the vcam's lookahead until you get everything else the way you like it. It just adds confusion.

    Also, you will run into gimbal-lock issues with transform.LookAt() and with the vcam's composer unless you prevent your player from going too steeply up or down. If you try, it will go crazy.

    CMBrain has a World-up override that you should set to player. That means that the player's local up will be used for up-sensitive calculations (like lookAt)... at least for the vcam. You will also have to pass the player's local up to transform.LookAt as a second parameter in your script to get everything to work.