Search Unity

Lerp certain variables in my script?

Discussion in 'Scripting' started by Treasureman, Nov 28, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a script that makes my gun sway in my game when I move the camera. Here's the script...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GunSway: MonoBehaviour {
    5.     public float moveAmount = 1f;
    6.     public float moveSpeed = 2f;
    7.     public GameObject gun;
    8.     private float moveOnX;
    9.     private float moveOnY;
    10.     public Vector3 defaultPosition;
    11.     public Vector3 newGunPosition;
    12.    
    13.    
    14.     // Use this for initialization
    15.     void Start () {
    16.        
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.        
    22.         moveOnX = Input.GetAxis("Mouse X") * Time.deltaTime * moveAmount;
    23.        
    24.         moveOnY = Input.GetAxis("Mouse Y") * Time.deltaTime * moveAmount;
    25.        
    26.         newGunPosition = new Vector3 (defaultPosition.x+moveOnX, defaultPosition.y+moveOnY, defaultPosition.z);
    27.        
    28.         gun.transform.localPosition = Vector3.Lerp(gun.transform.localPosition, newGunPosition, moveSpeed*Time.deltaTime);
    29.        
    30.     }
    31.    
    32.    
    33. }
    34.  
    But, I want to make it so when you hold in right click, it will lerp these variables

    defaultPosition: x = 0.465 changes to x = 0
    defaultPostion: y = -0.397 changes to y = -0.355
    moveAmount = 7 changes to 1

    I have a script that can change certain variables in Javascript coding, but it won't work with this CSharp script. This is that code...
    Code (JavaScript):
    1. var NormalX: float = 0.8; // XOffset while not aiming
    2. var AimX: float = 0.7; // XOffset while aiming
    3. private var cameraScript: MouseOrbitOTS;
    4. function Start(){
    5.      cameraScript = GetComponent(MouseOrbitOTS);
    6.      var ch:Camera = GetComponent(Camera);
    7. }
    8. function Update(){
    9.      var cameraX = NormalX;
    10.      if (Input.GetButton("Aim")){
    11.          cameraX = AimX;
    12.       }
    13.       cameraScript.xOffset = cameraX; // set XOffset while aiming or not aiming
    14. }
    What could I do to get the outcome I want?