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

Add another input to this script?

Discussion in 'Scripting' started by Treasureman, Feb 19, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a code that aligns my character controller with my camera. Here's the script...
    Code (JavaScript):
    1. var target : Transform;
    2. var distance = 10.0;
    3. var LastPos = new Vector3(0,0,0);
    4. var debug = 0.0;
    5. var xSpeed = 250.0;
    6. var ySpeed = 120.0;
    7. var yMinLimit = -20;
    8. var yMaxLimit = 80;
    9. private var x = 0.0;
    10. private var y = 0.0;
    11. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    12. function Start () {
    13.     var angles = transform.eulerAngles;
    14.     x = angles.y;
    15.     y = angles.x;
    16.     // Make the rigid body not change rotation
    17.        if (GetComponent.<Rigidbody>())
    18.         GetComponent.<Rigidbody>().freezeRotation = true;
    19. }
    20. function LateUpdate () {
    21.    var Magnitude = Vector3.Distance(target.position, LastPos);
    22.    debug = Magnitude;
    23.     if(Magnitude > 0.05)
    24.     {
    25.          target.localEulerAngles = new Vector3(target.localEulerAngles.x, Camera.main.transform.localEulerAngles.y, target.localEulerAngles.z);
    26.     }
    27.     LastPos = target.position;
    28.     if (target) {
    29.         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
    30.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    31.      
    32.          y = ClampAngle(y, yMinLimit, yMaxLimit);
    33.              
    34.         var rotation = Quaternion.Euler(y, x, 0);
    35.         var position = rotation * Vector3(0.9, 0.0, -distance) + target.position;
    36.      
    37.         transform.rotation = rotation;
    38.         transform.position = position;
    39.     }
    40. }
    41. static function ClampAngle (angle : float, min : float, max : float) {
    42.     if (angle < -360)
    43.         angle += 360;
    44.     if (angle > 360)
    45.         angle -= 360;
    46.     return Mathf.Clamp (angle, min, max);
    47. }
    But I want to add a new input to it. My problem is, that the other input isn't an axis. How would I add the input in so it works with it too. The input is called 'Aim'. Thanks!
     
    Last edited: Feb 19, 2015
  2. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    Try this:

    Code (csharp):
    1. Input.GetButtonDown ("Aim");
    2. //OR
    3. Input.GetButton ("Aim");
    4.  
    Now, using if you use this method, then you cannot do like:
    Code (csharp):
    1. Input.GetButton ("Aim") * aimSpeed;
    2.  
    The reason is because it is looking for you to press something, it isn't returning anything except that unless you use:
    Code (csharp):
    1. Input.GetAxis ("Aim");
    2.  
    PS: I am assuming "Aim" is used for aiming down the gun's sights, you COULD do an animation for that, just saying.

    This is what I thought you meant. If you already know about these but they don't work, please reply back!
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I know how to set up the input by itself, but I'm not sure how to get it to do same thing as the x and y axis in that script.