Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Camera System

Discussion in 'Scripting' started by Havie, May 11, 2020.

  1. Havie

    Havie

    Joined:
    Oct 12, 2019
    Posts:
    89
    Hello guys,

    I am trying to recreate the camera system from an older PS2 game. I can't seem to figure it out.
    If anyone has any advice please share.

    I have uploaded a video explaining


    Thanks for your time!
     
  2. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    183
  3. Havie

    Havie

    Joined:
    Oct 12, 2019
    Posts:
    89
    Thanks for the reply,
    I've actually managed to recreate this,
    and its a hell of a lot more complicated than just adding a camera as a child of the player and using look at.

    Theres a simplistic depth to this that doesnt look complex at first glance.
    The players movements are always based on the cameras forward, and the camera chases the player and tilts while holding "left" will run him in a circle in what feels like "around the camera"
    Its a pretty cool thing.

    For those looking to do something similar. You child the camera to a cube, and child that cube to the player. You then rotate that cube to rotate the camera.

    In a script when reading input you must get your input vector, and camera direction.
    Find the angle of input by trig/vector math then create a 4x4 rotation matrix.
    create a movement direction by using multiply vector on the normalized camera direction found.
    Then calculate the facing angle again off of the movement direction.
    finally apply this angle as a new vector3 with the x and z zerod out for your euler angles.
    Then the trick to making the camera chase you is to rotate the camera target if the abs value of ur horizontal read is above a threshold (i chose 0.01f), you add this to your MouseX.
    Which creates this effect where your camera driving the character movement
     
  4. ccbelan

    ccbelan

    Joined:
    May 7, 2020
    Posts:
    8
    hi are you using the new input system?
    If you could, can you share your script??
     
  5. Havie

    Havie

    Joined:
    Oct 12, 2019
    Posts:
    89
    Hey idk what the new input system is but here u go:


    Code (CSharp):
    1.  
    2.         if (!isAttacking)
    3.         {
    4.             // Input direction
    5.             float hori = Input.GetAxis("Horizontal");
    6.             float vert = Input.GetAxis("Vertical");
    7.             Vector3 inputVector = new Vector3(hori, 0.0f, vert);
    8.             Debug.DrawRay(transform.position, inputVector * 2, Color.red);
    9.  
    10.             // Camera direction
    11.             Vector3 cameraDirection = cameraTarget.position - myCamera.position;
    12.             cameraDirection.y = 0.0f; // we dont want up/down dir
    13.                                       //Vector3 option2= mainCamera.forward; //zero out
    14.             Debug.DrawRay(transform.position, cameraDirection.normalized * 2.0f, Color.green);
    15.  
    16.             // Movement angle
    17.             inputVector = inputVector.normalized;
    18.             float angle = Mathf.Atan2(inputVector.x, inputVector.z) / Mathf.PI * 180.0f;
    19.             Matrix4x4 rotation = Matrix4x4.Rotate(Quaternion.Euler(0f, angle, 0f));
    20.  
    21.             // Camera offset by input
    22.             Vector3 movementDirection = rotation.MultiplyVector(cameraDirection.normalized);
    23.             Debug.DrawRay(transform.position, movementDirection.normalized * 2.0f, Color.blue);
    24.  
    25.             if ( (Mathf.Abs(hori) > 1e-5 || Mathf.Abs(vert) > 1e-5) && !isAttacking)
    26.             {
    27.                 float facingAngle = Mathf.Atan2(movementDirection.x, movementDirection.z) / Mathf.PI * 180f;
    28.                 transform.eulerAngles = new Vector3(0.0f, facingAngle, 0.0f);
    29.                 _animator.SetBool("isMoving", true);
    30.  
    31.                 //Angle Camera - unused , now handled in cam script
    32.                 // cameraTarget.transform.Rotate(0, hori * 0.2f, 0);
    33.  
    34.                 return;
    35.  
    36.             }
    37.  
    38.         }
    39.         _animator.SetBool("isMoving", false);
     
  6. ccbelan

    ccbelan

    Joined:
    May 7, 2020
    Posts:
    8
    thanks!