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. Dismiss Notice

Camera and Player Follow ?

Discussion in 'Scripting' started by Giannigiardinelli, Nov 10, 2014.

  1. Giannigiardinelli

    Giannigiardinelli

    Joined:
    May 8, 2014
    Posts:
    179
    Hello, everyone!
    After several research forums, video, google, I ask you for help!
    I read several articles that could solve my problem, but nothing works!
    I try to make the camera follow the player?
    My camera is to left of my player and my cross in the center.

    when hovering over the camera left or right direction of my player, it does not move!

    I have try the script (MouseFollow) but its not good,
    Use another script (the forum here) and other problem ...

    I took the same kind of script that "3rd Person Shooter" in AssetStores unit
    So I'll send you the script I réutilliser my game, you can even download and watch the operation of the camera and you verais the camera rotates around the player but the player remains on the place!

    If anyone can help me to make the camera follow the direction of the player?
    I guess that will add mouse X and Y, but as a beginner, I'm not so sure?

    Forums.png

    I want the camera and the player follows the same direction with the direction of the mouse


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. // 3rd person game-like camera controller
    5. // keeps camera behind the player and aimed at aiming point
    6. public class ShooterGameCamera : MonoBehaviour {
    7.  
    8.     public Transform player;
    9.     public Transform aimTarget;
    10.  
    11.     public float smoothingTime = 0.5f;
    12.     public Vector3 pivotOffset = new Vector3(1.3f, 0.4f,  0.0f);
    13.     public Vector3 camOffset   = new Vector3(0.0f, 0.7f, -2.4f);
    14.     public Vector3 closeOffset = new Vector3(0.35f, 1.7f, 0.0f);
    15.  
    16.     public float horizontalAimingSpeed = 270f;
    17.     public float verticalAimingSpeed = 270f;
    18.     public float maxVerticalAngle = 80f;
    19.     public float minVerticalAngle = -80f;
    20.     public float mouseSensitivity = 0.1f;
    21.  
    22.     public Texture reticle;
    23.  
    24.     private float angleH = 0;
    25.     private float angleV = 0;
    26.     private Transform cam;
    27.     private float maxCamDist = 1;
    28.     private LayerMask mask;
    29.     private Vector3 smoothPlayerPos;
    30.     public float _rotationDamping = 10.0f;
    31.  
    32.  
    33.     // Use this for initialization
    34.     void Start ()
    35.     {
    36.         // Add player's own layer to mask
    37.         mask = 1 << player.gameObject.layer;
    38.         // Add Igbore Raycast layer to mask
    39.         mask |= 1 << LayerMask.NameToLayer("Ignore Raycast");
    40.         // Invert mask
    41.         mask = ~mask;
    42.      
    43.         cam = transform;
    44.         smoothPlayerPos = player.position;
    45.      
    46.         maxCamDist = 3;
    47.     }
    48.  
    49.     // Update is called once per frame
    50.     void LateUpdate () {
    51.         if (Time.deltaTime == 0 || Time.timeScale == 0 || player == null)
    52.             return;
    53.      
    54.         angleH += Mathf.Clamp(Input.GetAxis("Mouse X") + Input.GetAxis("Horizontal2"), -1, 1) * horizontalAimingSpeed * Time.deltaTime;
    55.      
    56.         angleV += Mathf.Clamp(Input.GetAxis("Mouse Y") + Input.GetAxis("Vertical2"), -1, 1) * verticalAimingSpeed * Time.deltaTime;
    57.         // limit vertical angle
    58.         angleV = Mathf.Clamp(angleV, minVerticalAngle, maxVerticalAngle);
    59.  
    60.         // Before changing camera, store the prev aiming distance.
    61.         // If we're aiming at nothing (the sky), we'll keep this distance.
    62.         float prevDist = (aimTarget.position - cam.position).magnitude;
    63.      
    64.         // Set aim rotation
    65.         Quaternion aimRotation = Quaternion.Euler(-angleV, angleH, 0);
    66.         Quaternion camYRotation = Quaternion.Euler(0, angleH, 0);
    67.         Quaternion desiredRotation = Quaternion.LookRotation(this.aimTarget.position - this.transform.position, this.aimTarget.up);
    68.      
    69.         this.transform.rotation = Quaternion.Slerp(this.transform.rotation, desiredRotation, Time.deltaTime * this._rotationDamping);
    70.  
    71.         cam.rotation = aimRotation;
    72.      
    73.         // Find far and close position for the camera
    74.         smoothPlayerPos = Vector3.Lerp(smoothPlayerPos, player.position, smoothingTime * Time.deltaTime);
    75.         smoothPlayerPos.x = player.position.x;
    76.         smoothPlayerPos.z = player.position.z;
    77.         Vector3 farCamPoint = smoothPlayerPos + camYRotation * pivotOffset + aimRotation * camOffset;
    78.         Vector3 closeCamPoint = player.position + camYRotation * closeOffset;
    79.         float farDist = Vector3.Distance(farCamPoint, closeCamPoint);
    80.      
    81.         // Smoothly increase maxCamDist up to the distance of farDist
    82.         maxCamDist = Mathf.Lerp(maxCamDist, farDist, 5 * Time.deltaTime);
    83.      
    84.         // Make sure camera doesn't intersect geometry
    85.         // Move camera towards closeOffset if ray back towards camera position intersects something
    86.         RaycastHit hit;
    87.         Vector3 closeToFarDir = (farCamPoint - closeCamPoint) / farDist;
    88.         float padding = 0.3f;
    89.         if (Physics.Raycast(closeCamPoint, closeToFarDir, out hit, maxCamDist + padding, mask)) {
    90.             maxCamDist = hit.distance - padding;
    91.         }
    92.         cam.position = closeCamPoint + closeToFarDir * maxCamDist;
    93.      
    94.         // Do a raycast from the camera to find the distance to the point we're aiming at.
    95.         float aimTargetDist;
    96.         if (Physics.Raycast(cam.position, cam.forward, out hit, 100, mask)) {
    97.             aimTargetDist = hit.distance + 0.05f;
    98.         }
    99.         else {
    100.             // If we're aiming at nothing, keep prev dist but make it at least 5.
    101.             aimTargetDist = Mathf.Max(5, prevDist);
    102.         }
    103.      
    104.         // Set the aimTarget position according to the distance we found.
    105.         // Make the movement slightly smooth.
    106.         aimTarget.position = cam.position + cam.forward * aimTargetDist;
    107.     }
    108.  
    109.     void OnGUI () {
    110.         if (Time.time != 0 && Time.timeScale != 0)
    111.             GUI.DrawTexture(new Rect(Screen.width/2-(reticle.width*0.5f), Screen.height/2-(reticle.height*0.5f), reticle.width, reticle.height), reticle);
    112.     }
    113. }


    Thank you in advance


    [1]: /storage/temp/34968-forums.png
     
    Last edited: Nov 10, 2014
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Je ne sais pas si tu vas trouver de l'aide ici. Le forum est principalement anglophone.

    D'ailleurs, pourrais-tu mettre ton code entre les balises [CODE ] [/CODE](Sans espace)
    Ça sera plus facile à lire. :)
     
  3. Giannigiardinelli

    Giannigiardinelli

    Joined:
    May 8, 2014
    Posts:
    179
    OUPS
    Its better now :)
     
  4. Giannigiardinelli

    Giannigiardinelli

    Joined:
    May 8, 2014
    Posts:
    179
  5. Giannigiardinelli

    Giannigiardinelli

    Joined:
    May 8, 2014
    Posts:
    179