Search Unity

Camera Shake While Shooting

Discussion in 'Scripting' started by mainmatrix, Jul 8, 2012.

  1. mainmatrix

    mainmatrix

    Joined:
    Jul 3, 2012
    Posts:
    2
    I am using the attached code from ETeeskiTutorials. I would like to apply camera shake to the camera using random.value every time a bullet is fired. Just not sure how to do it..

    ET's tutorial applies Random.value to the gun model and I can't figure out how to apply it to the camera.


    #pragma strict

    var cameraObject : GameObject;
    @HideInInspector
    var targetXRotation : float;
    @HideInInspector
    var targetYRotation : float;
    @HideInInspector
    var targetXRotationV : float;
    @HideInInspector
    var targetYRotationV : float;

    var rotateSpeed : float = 0.3;

    var holdHeight : float = -0.5;
    var holdSide : float = 0.5;
    var holdFront : float = 0.5;

    var ratioHipHold : float = 1;
    var hipToAimSpeed : float = 0.1;
    @HideInInspector
    var ratioHipHoldV : float;

    var aimRatio : float = 0.4;

    var zoomAngle : float = 30;

    var fireSpeed : float = 15;
    @HideInInspector
    var waitTilNextFire : float = 0;
    var bullet : GameObject;
    var bulletSpawn : GameObject;

    var bulletSpreadAiming : float = 5;
    var bulletSpreadNotAiming : float = 15;


    var recoilAmountAiming : float = 0.1;
    var recoilAmountNotAiming : float = 0.2;

    var recoilRecoverTime : float = 0.2;
    @HideInInspector
    var currentRecoilZPos : float;
    @HideInInspector
    var currentRecoilZPosV : float;

    @HideInInspector
    var currentRecoilAimingZPos : float;
    @HideInInspector
    var currentRecoilAimingZPosV : float;

    var bulletSound : GameObject;



    function Update ()

    {
    var holdSound : GameObject;



    if (Input.GetButton("Fire1"))

    {
    if (waitTilNextFire <= 0)

    {
    if (bullet)
    Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    if (bulletSound)
    holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);



    currentRecoilZPos -= recoilAmountNotAiming;
    currentRecoilAimingZPos -= recoilAmountAiming;




    waitTilNextFire = 1;
    }

    }

    waitTilNextFire -= Time.deltaTime * fireSpeed;

    if (holdSound)
    holdSound.transform.parent = transform;

    currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
    currentRecoilAimingZPos = Mathf.SmoothDamp( currentRecoilAimingZPos, 0, currentRecoilAimingZPosV, recoilRecoverTime);


    cameraObject.GetComponent(MouseLookScript).currentTargetCameraAngle = zoomAngle;

    if (Input.GetButton("Fire2"))
    {
    cameraObject.GetComponent(MouseLookScript).currentAimRatio = aimRatio;
    ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 0, ratioHipHoldV, hipToAimSpeed);
    }

    if (Input.GetButton("Fire2") == false)
    {
    cameraObject.GetComponent(MouseLookScript).currentAimRatio = 1;
    ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 1, ratioHipHoldV, hipToAimSpeed);
    }


    transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector4(holdSide * ratioHipHold, holdHeight * ratioHipHold, holdFront * ratioHipHold, 0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0, 0, currentRecoilZPos));

    targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
    targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);

    transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);

    }
     
  2. SlainPriest

    SlainPriest

    Joined:
    Jul 8, 2012
    Posts:
    6
    Try hitting the button with the pound sign on it, and put your code inside of that. It'll be a lot less harsh for people to read.

    Here is a bit simpler camera shake script (it's much lighter, and you're using it for firing the gun so the shake doesn't need to be super complex anyways) from one of our Reddit friends, mikedoesweb. See if you like it. Basically, just run the Shake function every time you shoot the gun.

    Code (csharp):
    1. var originPosition:Vector3;
    2. var originRotation:Quaternion;
    3.  
    4. var shake_decay: float;
    5. var shake_intensity: float;;
    6.  
    7. function OnGUI () {
    8.     if (GUI.Button (Rect (20,40,80,20), "Shake")) {
    9.         Shake();
    10.     }
    11. }
    12.  
    13. function Update(){
    14.     if(shake_intensity > 0){
    15.         transform.position = originPosition + Random.insideUnitSphere * shake_intensity;
    16.         transform.rotation =  Quaternion(
    17.                         originRotation.x + Random.Range(-shake_intensity,shake_intensity)*.2,
    18.                         originRotation.y + Random.Range(-shake_intensity,shake_intensity)*.2,
    19.                         originRotation.z + Random.Range(-shake_intensity,shake_intensity)*.2,
    20.                         originRotation.w + Random.Range(-shake_intensity,shake_intensity)*.2);
    21.         shake_intensity -= shake_decay;
    22.     }
    23. }
    24.  
    25. function Shake(){
    26.     originPosition = transform.position;
    27.     originRotation = transform.rotation;
    28.     shake_intensity = .3;
    29.     shake_decay = 0.002;
    30. }
     
  3. Colmod

    Colmod

    Joined:
    Jan 9, 2022
    Posts:
    7
    upload_2022-6-4_18-33-46.png
     
  4. chrische5

    chrische5

    Joined:
    Oct 12, 2015
    Posts:
    52
    Hello

    And? Btw take a Look to the date.

    Christoph