Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Lookat mousePosition goes bananas

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

  1. ngerbens

    ngerbens

    Joined:
    Nov 20, 2017
    Posts:
    14
    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:
    14
    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:
    1,733
    Can you show me the inspector for your virtual camera?
     
  4. ngerbens

    ngerbens

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

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

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,733
    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.