Search Unity

Bootcamp Soldier Camera Script

Discussion in 'Scripting' started by christadwani, Apr 26, 2013.

  1. christadwani

    christadwani

    Joined:
    Apr 13, 2013
    Posts:
    7
    i tried using the scripts of the Bootcamp Soldier on another model in a different project so i got many errors but i got one that i couldn't fix
    Assets/Standard Assets/Scripts/SoldierCamera.js(1,6): UCE0001: ';' expected. Insert a semicolon at the end.
    Assets/Standard Assets/Scripts/SoldierCamera.js(2,6): UCE0001: ';' expected. Insert a semicolon at the end.
    Assets/Standard Assets/Scripts/SoldierCamera.js(4,1): BCE0044: expecting EOF, found 'strict'.
    here's the script : (i added the first two lines myself because there was another error and this fixed it
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. #pragma strict
    5. #pragma implicit
    6. #pragma downcast
    7.  
    8. class SoldierCamera extends MonoBehaviour
    9. {
    10.     public var target : Transform;
    11.     public var soldier : Transform;
    12.    
    13.     public var speed : Vector2 = new Vector2(135.0, 135.0);
    14.     public var aimSpeed : Vector2 = new Vector2(70.0, 70.0);
    15.     public var maxSpeed : Vector2 = new Vector2(100.0, 100.0);
    16.  
    17.     public var yMinLimit = -90;
    18.     public var yMaxLimit = 90;
    19.    
    20.     public var normalFOV = 60;
    21.     public var zoomFOV = 30;
    22.    
    23.     public var lerpSpeed = 8.0;
    24.    
    25.     private var distance = 10.0;
    26.  
    27.     private var x = 0.0;
    28.     public var y = 0.0;
    29.    
    30.     private var camTransform : Transform;
    31.     private var rotation : Quaternion;
    32.     private var position : Vector3;
    33.     private var deltaTime : float;
    34.     private var originalSoldierRotation : Quaternion;
    35.    
    36.     private var soldierController : SoldierController;
    37.    
    38.     public var orbit : boolean;
    39.    
    40.     public var hitLayer : LayerMask;
    41.    
    42.     private var cPos : Vector3;
    43.    
    44.     public var normalDirection : Vector3;
    45.     public var aimDirection : Vector3;
    46.     public var crouchDirection : Vector3;
    47.     public var aimCrouchDirection : Vector3;
    48.    
    49.     public var positionLerp : float;
    50.    
    51.     public var normalHeight : float;
    52.     public var crouchHeight : float;
    53.     public var normalAimHeight : float;
    54.     public var crouchAimHeight : float;
    55.     public var minHeight : float;
    56.     public var maxHeight : float;
    57.    
    58.     public var normalDistance : float;
    59.     public var crouchDistance : float;
    60.     public var normalAimDistance : float;
    61.     public var crouchAimDistance : float;
    62.     public var minDistance : float;
    63.     public var maxDistance : float;
    64.    
    65.     private var targetDistance : float;
    66.     private var camDir : Vector3;
    67.     private var targetHeight : float;
    68.    
    69.    
    70.     public var minShakeSpeed : float;
    71.     public var maxShakeSpeed : float;
    72.    
    73.     public var minShake : float;
    74.     public var maxShake : float = 2.0;
    75.    
    76.     public var minShakeTimes : int;
    77.     public var maxShakeTimes : int;
    78.    
    79.     public var maxShakeDistance : float;
    80.    
    81.     private var shake : boolean;
    82.     private var shakeSpeed : float = 2.0;
    83.     private var cShakePos : float;
    84.     private var shakeTimes : int = 8;
    85.     private var cShake : float;
    86.     private var cShakeSpeed : float;
    87.     private var cShakeTimes : int;
    88.    
    89.     public var radar : Transform;
    90.     public var radarCamera : Transform;
    91.  
    92.     private var _depthOfFieldEffect : DepthOfField;
    93.    
    94.     function Start ()
    95.     {  
    96.         cShakeTimes = 0;
    97.         cShake = 0.0;
    98.         cShakeSpeed = shakeSpeed;
    99.        
    100.         _depthOfFieldEffect = gameObject.GetComponent("DepthOfField") as DepthOfField;
    101.  
    102.         if(target == null || soldier == null)
    103.         {
    104.             Destroy(this);
    105.             return;
    106.         }
    107.        
    108.         target.parent = null;
    109.        
    110.         camTransform = transform;
    111.        
    112.         var angles = camTransform.eulerAngles;
    113.         x = angles.y;
    114.         y = angles.x;
    115.        
    116.         originalSoldierRotation = soldier.rotation;
    117.        
    118.         soldierController = soldier.GetComponent("SoldierController");
    119.        
    120.         targetDistance = normalDistance;
    121.        
    122.         cPos = soldier.position + new Vector3(0, normalHeight, 0);
    123.     }
    124.    
    125.     function GoToOrbitMode(state : boolean)
    126.     {
    127.         orbit = state;
    128.        
    129.         soldierController.idleTimer = 0.0;
    130.     }
    131.    
    132.     function Update()
    133.     {
    134.         if(GameManager.pause || GameManager.scores) return;
    135.         //if(GameManager.scores) return;
    136.  
    137.         if(orbit  (Input.GetKeyDown(KeyCode.O) || Input.GetAxis("Horizontal") != 0.0 || Input.GetAxis("Vertical") != 0.0 || soldierController.aim || soldierController.fire))
    138.         {
    139.             GoToOrbitMode(false);
    140.         }
    141.        
    142.         if(!orbit  soldierController.idleTimer > 0.1)
    143.         {
    144.             GoToOrbitMode(true);
    145.         }
    146.     }
    147.    
    148.     function LateUpdate ()
    149.     {
    150.         //if(GameManager.pause || GameManager.scores) return;
    151.         if(GameManager.scores) return;
    152.  
    153.         deltaTime = Time.deltaTime;
    154.        
    155.         GetInput();
    156.        
    157.         RotateSoldier();
    158.  
    159.         CameraMovement();
    160.  
    161.         DepthOfFieldControl();
    162.     }
    163.    
    164.     function CameraMovement()
    165.     {
    166.         if(soldierController.aim)
    167.         {
    168.             (camera.GetComponent(DepthOfField) as DepthOfField).enabled = true;
    169.             camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoomFOV, deltaTime * lerpSpeed);
    170.            
    171.             if(soldierController.crouch)
    172.             {
    173.                 camDir = (aimCrouchDirection.x * target.forward) + (aimCrouchDirection.z * target.right);
    174.                 targetHeight = crouchAimHeight;
    175.                 targetDistance = crouchAimDistance;
    176.             }
    177.             else
    178.             {
    179.                 camDir = (aimDirection.x * target.forward) + (aimDirection.z * target.right);
    180.                 targetHeight = normalAimHeight;
    181.                 targetDistance = normalAimDistance;
    182.             }
    183.         }
    184.         else
    185.         {
    186.             (camera.GetComponent(DepthOfField) as DepthOfField).enabled = false;
    187.             camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, normalFOV, deltaTime * lerpSpeed);
    188.            
    189.             if(soldierController.crouch)
    190.             {
    191.                 camDir = (crouchDirection.x * target.forward) + (crouchDirection.z * target.right);
    192.                 targetHeight = crouchHeight;
    193.                 targetDistance = crouchDistance;
    194.             }
    195.             else
    196.             {
    197.                 camDir = (normalDirection.x * target.forward) + (normalDirection.z * target.right);
    198.                 targetHeight = normalHeight;
    199.                 targetDistance = normalDistance;
    200.             }
    201.         }
    202.        
    203.         camDir = camDir.normalized;
    204.        
    205.         HandleCameraShake();
    206.        
    207.         cPos = soldier.position + new Vector3(0, targetHeight, 0);
    208.        
    209.         var hit : RaycastHit;
    210.         if(Physics.Raycast(cPos, camDir, hit, targetDistance + 0.2, hitLayer))
    211.         {
    212.             var t : float = hit.distance - 0.1;
    213.             t -= minDistance;
    214.             t /= (targetDistance - minDistance);
    215.  
    216.             targetHeight = Mathf.Lerp(maxHeight, targetHeight, Mathf.Clamp(t, 0.0, 1.0));
    217.             cPos = soldier.position + new Vector3(0, targetHeight, 0);
    218.         }
    219.        
    220.         if(Physics.Raycast(cPos, camDir, hit, targetDistance + 0.2, hitLayer))
    221.         {
    222.             targetDistance = hit.distance - 0.1;
    223.         }
    224.         if(radar != null)
    225.         {
    226.             radar.position = cPos;
    227.             radarCamera.rotation = Quaternion.Euler(90, x, 0);
    228.         }
    229.        
    230.         var lookPoint : Vector3 = cPos;
    231.         lookPoint += (target.right * Vector3.Dot(camDir * targetDistance, target.right));
    232.  
    233.         camTransform.position = cPos + (camDir * targetDistance);
    234.         camTransform.LookAt(lookPoint);
    235.        
    236.         target.position = cPos;
    237.         target.rotation = Quaternion.Euler(y, x, 0);
    238.     }
    239.    
    240.     function HandleCameraShake()
    241.     {
    242.         if(shake)
    243.         {
    244.             cShake += cShakeSpeed * deltaTime;
    245.            
    246.             if(Mathf.Abs(cShake) > cShakePos)
    247.             {
    248.                 cShakeSpeed *= -1.0;
    249.                 cShakeTimes++;
    250.                
    251.                 if(cShakeTimes >= shakeTimes)
    252.                 {
    253.                     shake = false;
    254.                 }
    255.                
    256.                 if(cShake > 0.0)
    257.                 {
    258.                     cShake = maxShake;
    259.                 }
    260.                 else
    261.                 {
    262.                     cShake = -maxShake;
    263.                 }
    264.             }
    265.            
    266.             targetHeight += cShake;
    267.         }
    268.     }
    269.    
    270.     function StartShake(distance : float)
    271.     {
    272.         var proximity : float = distance / maxShakeDistance;
    273.        
    274.         if(proximity > 1.0) return;
    275.        
    276.         proximity = Mathf.Clamp(proximity, 0.0, 1.0);
    277.        
    278.         proximity = 1.0 - proximity;
    279.        
    280.         cShakeSpeed = Mathf.Lerp(minShakeSpeed, maxShakeSpeed, proximity);
    281.         shakeTimes = Mathf.Lerp(minShakeTimes, maxShakeTimes, proximity);
    282.         cShakeTimes = 0;
    283.         cShakePos = Mathf.Lerp(minShake, maxShake, proximity);
    284.        
    285.         shake = true;
    286.     }
    287.  
    288.     function GetInput()
    289.     {
    290.         var a : Vector2 = soldierController.aim ? aimSpeed : speed;
    291.         x += Mathf.Clamp(Input.GetAxis("Mouse X") * a.x, -maxSpeed.x, maxSpeed.x) * deltaTime;
    292.         y -= Mathf.Clamp(Input.GetAxis("Mouse Y") * a.y, -maxSpeed.y, maxSpeed.y) * deltaTime;
    293.         y = ClampAngle(y, yMinLimit, yMaxLimit);
    294.     }
    295.  
    296.     function DepthOfFieldControl()
    297.     {
    298.         if(_depthOfFieldEffect == null) return;
    299.         if(soldierController == null) return;
    300.        
    301.         if(soldierController.aim  GameQualitySettings.depthOfField)
    302.         {
    303.             if(!_depthOfFieldEffect.enabled)
    304.             {
    305.                 _depthOfFieldEffect.enabled = true;
    306.             }
    307.         }
    308.         else
    309.         {
    310.             if(_depthOfFieldEffect.enabled)
    311.             {
    312.                 _depthOfFieldEffect.enabled = false;
    313.             }
    314.         }
    315.     }
    316.    
    317.     function RotateSoldier()
    318.     {
    319.         if(!orbit)
    320.             soldierController.targetYRotation = x;
    321.     }
    322.    
    323.     static function ClampAngle(angle : float, min : float, max : float) : float
    324.     {
    325.         if (angle < -360)
    326.         {
    327.             angle += 360;
    328.         }
    329.        
    330.         if (angle > 360)
    331.         {
    332.             angle -= 360;
    333.         }
    334.        
    335.         return Mathf.Clamp (angle, min, max);
    336.     }
    337. }
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    The script is in UnityScript, but the first two lines are C#. Delete them.
     
  3. christadwani

    christadwani

    Joined:
    Apr 13, 2013
    Posts:
    7
    thanks but, i get another error
    Assets/Standard Assets/Scripts/SoldierCamera.js(89,39): BCE0018: The name 'DepthOfField' does not denote a valid type ('not found').
    Assets/Standard Assets/Scripts/SoldierCamera.js(97,74): BCE0018: The name 'DepthOfField' does not denote a valid type ('not found').
    Assets/Standard Assets/Scripts/SoldierCamera.js(165,63): BCE0018: The name 'DepthOfField' does not denote a valid type ('not found').
    Assets/Standard Assets/Scripts/SoldierCamera.js(183,63): BCE0018: The name 'DepthOfField' does not denote a valid type ('not found').
    Assets/Standard Assets/Scripts/SoldierController.js(22,35): BCE0018: The name 'GunManager' does not denote a valid type ('not found').
    Assets/Standard Assets/Scripts/WaterInteractions.js(245,27): BCE0018: The name 'AutoDestroy' does not denote a valid type ('not found'). Did you mean 'UnityEngine.AudioDistortionFilter'?
    Assets/Standard Assets/Scripts/WaterInteractions.js(245,75): BCE0018: The name 'AutoDestroy' does not denote a valid type ('not found'). Did you mean 'UnityEngine.AudioDistortionFilter'?
     
  4. FrowningPigeon

    FrowningPigeon

    Joined:
    Jun 28, 2013
    Posts:
    6
    Hi,

    I was just wondering if you managed to get the code working? I am trying to get this sort of camera myself...and would be helpful if you could post the code if you got it working?

    Thanks
     
  5. christadwani

    christadwani

    Joined:
    Apr 13, 2013
    Posts:
    7
  6. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42
    not is need these 2 first lines in script,

    this isn't need in js script,

    only C#Script

    Sorry my bad english :(
     
  7. subho0406

    subho0406

    Joined:
    Mar 20, 2014
    Posts:
    104
    In newer version of unity you need to import the image effects package then replace all depthoffield by depthoffield34. It appears that you deleted some dependent scripts such as gunmanager.
     
unityunity