Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  3. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Top Down Camera and world-relative directions of the character

Discussion in 'Cinemachine' started by PekoLin, Dec 1, 2018.

  1. PekoLin

    PekoLin

    Joined:
    Jan 13, 2017
    Posts:
    2
    Hi everyone,
    I have been trying to use Cinemachine and Unity-Standard Package to make some experimental game..since I have very limited coding experience, this might be a super beginner question for you. But I hope you guys can help me..thank you! (and apologize for my english..)

    So I am using the Third person user control from standard package, and now I set the player's camera in Timeline, with first simple follow v-camera and than switch to top down v-camera, while the character is running.

    But every time when the camera switch to top down, the player started to running opposite (so W become S).

    I checked the code, but can only guess is the problem of world-relative direction..but I don't know how to fix it. Although, I wasn't sure if this is because of the player control or the cinemachine. Hope anyone here can guild me a way, thank you!


    Code (CSharp):
    1. // read inputs
    2.             float h = CrossPlatformInputManager.GetAxis("Horizontal");
    3.             float v = CrossPlatformInputManager.GetAxis("Vertical");
    4.             bool crouch = Input.GetKey(KeyCode.C);
    5.  
    6.  
    7.             // calculate move direction to pass to character
    8.             if (m_Cam != null)
    9.             {
    10.                 // calculate camera relative direction to move:
    11.                 m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
    12.                 m_Move = v*m_CamForward + h*m_Cam.right;
    13.             }
    14.             else
    15.             {
    16.                 // we use world-relative directions in the case of no main camera
    17.                 m_Move = v*Vector3.back + h*Vector3.right;
    18.             }
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,823
    I assume that in your case m_Cam != null.
    When you say top-down, is the camera really pointing down, or is it at an angle?
    If it's really down, then you will not be able to determine an accurate direction in line 11. That would explain the problem you're describing.
     
  3. PekoLin

    PekoLin

    Joined:
    Jan 13, 2017
    Posts:
    2
    Thank you for your reply,
    I actually simply rotate the camera to have top down perspective, is there another way to do it?
    (Like the world up override will do, but I didn't want to have top down the whole time)
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,823
    You don't need the world up override, rotating the vcam is fine.

    However, if you rotate the vcam 90 degrees so that it's pointing straight down, your method for calculating motion forward will fail. That's just a consequence of the math. To fix it, use less than 90 degrees, say 80.