Search Unity

[Solved] Any Reason My Vertical Axis Input Not Working?

Discussion in 'Scripting' started by GhulamJewel, Jun 13, 2018.

  1. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Edit* Looks like the map I was following on the internet was wrong and vertical axis is 6th not the 4th.



    upload_2018-6-13_2-12-54.png upload_2018-6-13_2-12-12.png

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.    //[AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]
    5.     public class MouseOrbit : MonoBehaviour
    6. {
    7.     public Transform target;
    8.     public float distance = 5.0f;
    9.     public float xSpeed = 120.0f;
    10.     public float ySpeed = 120.0f;
    11.  
    12.     public float yMinLimit = -20f;
    13.     public float yMaxLimit = 80f;
    14.  
    15.     public float distanceMin = .5f;
    16.     public float distanceMax = 15f;
    17.  
    18.     public float smoothTime = 2f;
    19.  
    20.     float rotationYAxis = 0.0f;
    21.     float rotationXAxis = 0.0f;
    22.  
    23.     float velocityX = 0.0f;
    24.     float velocityY = 0.0f;
    25.  
    26.     public float Seconds = 0.5f;
    27.  
    28.     // Use this for initialization
    29.     void Start()
    30.     {
    31.  
    32.         Vector3 angles = transform.eulerAngles;
    33.         rotationYAxis = angles.y;
    34.         rotationXAxis = angles.x;
    35.  
    36.         // Make the rigid body not change rotation
    37.                   if (GetComponent<Rigidbody>())
    38.         {
    39.             GetComponent<Rigidbody>().freezeRotation = true;
    40.         }
    41.     }
    42.  
    43.         void LateUpdate()
    44.     {
    45.         if (target)
    46.         {
    47.             if (ControlFreak2.CF2Input.GetMouseButton (0)) {
    48.                 velocityX += xSpeed * ControlFreak2.CF2Input.GetAxis ("Mouse X") * 0.02f;
    49.                 velocityY += ySpeed * ControlFreak2.CF2Input.GetAxis ("Mouse Y") * 0.02f;
    50.  
    51.             }else{
    52.  
    53.                 velocityX += xSpeed * Input.GetAxis ("Horizontal2") * 0.02f;
    54.                 velocityY += ySpeed * Input.GetAxis ("Vertical2") * 0.02f;
    55.  
    56.             }
    57.        
    58.             rotationYAxis += velocityX;
    59.             rotationXAxis -= velocityY;
    60.    
    61.             rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
    62.      
    63.             Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
    64.             Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
    65.             Quaternion rotation = toRotation;
    66.    
    67.             distance = Mathf.Clamp(distance - ControlFreak2.CF2Input.GetAxis("Mouse ScrollWheel")*Seconds, distanceMin, distanceMax);
    68.        
    69.        
    70.             Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
    71.             Vector3 position = rotation * negDistance + target.position;
    72.                  
    73.             transform.rotation = rotation;
    74.             transform.position = position;
    75.    
    76.             velocityX = Mathf.Lerp(velocityX, 0, Time.unscaledDeltaTime * smoothTime);
    77.             velocityY = Mathf.Lerp(velocityY, 0, Time.unscaledDeltaTime * smoothTime);
    78.         }
    79.  
    80.     }
    81.  
    82.           public static float ClampAngle(float angle, float min, float max)
    83.     {
    84.         if (angle < -360F)
    85.             angle += 360F;
    86.         if (angle > 360F)
    87.             angle -= 360F;
    88.         return Mathf.Clamp(angle, min, max);
    89.     }
    90. }
    91.  

    Just trying to add controller support to a project I am working on. The mouse orbit script works with mouse and keyboard and I have added controller support. Horizontal axis work on the PS4 right stick but not the vertical?
     

    Attached Files:

    Last edited: Jun 13, 2018