Search Unity

error CS1525: Unexpected symbol `else'

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

  1. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Trying to create a nested else statements but getting this error? Any advise what to remove or include? Thank you.

    MouseOrbit.cs(55,5): error CS1525: Unexpected symbol `else'



    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.             if (ControlFreak2.CF2Input.GetMouseButton (0)) {
    47.                 velocityX += xSpeed * ControlFreak2.CF2Input.GetAxis ("Mouse X") * 0.02f;
    48.                 velocityY += ySpeed * ControlFreak2.CF2Input.GetAxis ("Mouse Y") * 0.02f;
    49.  
    50.             } else {
    51.  
    52.                 velocityX += xSpeed * Input.GetAxis ("Horizontal2") * 0.02f;
    53.                 velocityY += ySpeed * Input.GetAxis ("Vertical2") * 0.02f;
    54.  
    55.             } else {
    56.  
    57.             velocityX += xSpeed * Input.GetAxis ("Horizontal3") * 0.02f;
    58.             velocityY += ySpeed * Input.GetAxis ("Vertical3") * 0.02f;
    59.  
    60.         }
    61.    
    62.             rotationYAxis += velocityX;
    63.             rotationXAxis -= velocityY;
    64.            
    65.             rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
    66.              
    67.             Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
    68.             Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
    69.             Quaternion rotation = toRotation;
    70.            
    71.             distance = Mathf.Clamp(distance - ControlFreak2.CF2Input.GetAxis("Mouse ScrollWheel")*Seconds, distanceMin, distanceMax);    
    72.                
    73.                
    74.             Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
    75.             Vector3 position = rotation * negDistance + target.position;
    76.                          
    77.             transform.rotation = rotation;
    78.             transform.position = position;
    79.            
    80.             velocityX = Mathf.Lerp(velocityX, 0, Time.unscaledDeltaTime * smoothTime);
    81.             velocityY = Mathf.Lerp(velocityY, 0, Time.unscaledDeltaTime * smoothTime);
    82.         }
    83.        
    84.     }
    85.    
    86.           public static float ClampAngle(float angle, float min, float max)
    87.     {
    88.         if (angle < -360F)
    89.             angle += 360F;
    90.         if (angle > 360F)
    91.             angle -= 360F;
    92.         return Mathf.Clamp(angle, min, max);
    93.     }
    94. }
    95.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    You can't have two else statements with a single if statement.

    Correct way
    If
    else if
    else

    incorrect
    if
    else
    else
     
    GhulamJewel likes this.
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    They don't nest that way. There can be zero or many "else if" but only zero or one "else" statements per "if" :
    Code (CSharp):
    1. if( condition_1 )
    2. {
    3. }
    4. else if ( condition_2 )
    5. {
    6. }
    7. else if ( condition_n )
    8. {
    9. }
    10. else
    11. {  // If none of the above then do this.
    12. }
     
    GhulamJewel likes this.
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Yeah, what @Doug_B said. In a nicer formatted less lazy way than me. :)
     
  5. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Thanks I did try something like that but is there a way to have no condition with "else if" basically it is just a camera script that works with mouse and keyboard as well as Xbox and PS4 controllers. The annoying thing the controllers have two different axis names hence why doing two different else. First one does have a condition because have to click mouse then move camera but the controllers should just work by moving the right analog stick. It works with only one else statement but not sure how to do it with two. Thanks.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    There is no way to have two else. You should have another condition you should be checking for, some check for the two sticks.

    But as you have it written, do you need two else statements? Would putting all that code work? Cause unless you have a condition to check against, you're currently just telling it to check for one condition, if that isn't true, then run this other code down here. Since that is all an else does.
     
    Doug_B likes this.
  7. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Thanks there is a plugin called InControl which I might use as solves all your input problems. I am hesitate because I am already using Controlfreak plugin but will experiment. I Did try something like below to get one or another axis with the || but I get (53,77): error CS1525: Unexpected symbol `+='. Thanks for your advise!

    Code (CSharp):
    1.         if (target)
    2.         {
    3.             if (ControlFreak2.CF2Input.GetMouseButton (0)) {
    4.                 velocityX += xSpeed * ControlFreak2.CF2Input.GetAxis ("Mouse X") * 0.02f;
    5.                 velocityY += ySpeed * ControlFreak2.CF2Input.GetAxis ("Mouse Y") * 0.02f;
    6.  
    7.             }else{
    8.  
    9.                 velocityX += xSpeed * Input.GetAxis ("Horizontal2") * 0.02f || velocityX += xSpeed * Input.GetAxis ("Horizontal3") * 0.02f;
    10.                 velocityY += ySpeed * Input.GetAxis ("Vertical2") * 0.02f || velocityX += xSpeed * Input.GetAxis ("Vertical3") * 0.02f;
    11.  
    12.             }
    .
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hmm.. maybe something like (only showing the horizontal portion):
    Code (csharp):
    1. else {
    2.    float horz = Input.GetAxis("Horizontal2");
    3.  
    4.    if(horz == 0) horz = Input.GetAxis("Horizontal3");
    5.  
    6.    velocityX += xSpeed * horz * 0.02f;
    7. }
    That assumes you don't know which controller might be in use, but should work.
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Yeah, @methos5k posted a similar thought to what I had.

    But, I think Input.GetAxis always returns 0 if it is just at base position also, doesn't it?

    I'm assuming -1 is one direction at max and 1 is the other. So if you get 0, I suppose you just don't do anything with it.

    I haven't tried to hook into a controller yet, so I'm just thinking of the logic of it.
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Exactly.. I've never actually used a controller. My code would be a naive sort of check, seeing if 1 controller gives you any data, and only if not, check the other one. You'd still have zero if they neither were 'moved', I imagine.

    My other idea was to get them both and clamp them, but that seems a bit silly. :)
     
  11. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You are missing the closing bracket for if (target)
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That might have just been omitted from the post.
    This line of code doesn't look right.
    Code (csharp):
    1. velocityX += xSpeed * Input.GetAxis ("Horizontal2") * 0.02f || velocityX += xSpeed * Input.GetAxis ("Horizontal3") * 0.02f;
    :)
     
  13. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @methos5k Agreed, best to always post all the code. As you point out, in pseudocode, velocityX is equal to something or (and at the same time, ignoring the or) velocityX is equal to something else. Yeah, not quite right.

    Might be best to first write out the logic in pseudocode (sentences), then code it.
     
  14. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Thank you it seems to be close but still get some problems. The 360 axis works now. So

    Horizontal 2/ Vertical 2 = PS4 Axis
    Horizontal 3/Vertical 3 = Xbox 360/Xbox One Axis

    The Inputs are set up correctly. The problem seems now that if the PS4 pad is in use the camera moves horizontally uncontrollably so it keeps spinning in one direction. If not in use and unplugged the Xbox axis work fine. If the PS4 pad is in use with the Xbox unplugged it does the spinning uncontrollable behaviour? Any advise why?

    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.                 float horz = Input.GetAxis("Horizontal2");
    54.  
    55.                 if(horz == 0) horz = Input.GetAxis("Horizontal3");
    56.  
    57.                 velocityX += xSpeed * horz * 0.02f;
    58.  
    59.                 float vert = Input.GetAxis("Vertical2");
    60.  
    61.                 if(vert == 0) vert = Input.GetAxis("Vertical3");
    62.  
    63.                 velocityY += ySpeed * vert * 0.02f;
    64.  
    65.             }
    66.              
    67.             rotationYAxis += velocityX;
    68.             rotationXAxis -= velocityY;
    69.          
    70.             rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
    71.            
    72.             Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
    73.             Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
    74.             Quaternion rotation = toRotation;
    75.          
    76.             distance = Mathf.Clamp(distance - ControlFreak2.CF2Input.GetAxis("Mouse ScrollWheel")*Seconds, distanceMin, distanceMax);    
    77.              
    78.              
    79.             Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
    80.             Vector3 position = rotation * negDistance + target.position;
    81.                        
    82.             transform.rotation = rotation;
    83.             transform.position = position;
    84.          
    85.             velocityX = Mathf.Lerp(velocityX, 0, Time.unscaledDeltaTime * smoothTime);
    86.             velocityY = Mathf.Lerp(velocityY, 0, Time.unscaledDeltaTime * smoothTime);
    87.         }
    88.      
    89.     }
    90.  
    91.           public static float ClampAngle(float angle, float min, float max)
    92.     {
    93.         if (angle < -360F)
    94.             angle += 360F;
    95.         if (angle > 360F)
    96.             angle -= 360F;
    97.         return Mathf.Clamp(angle, min, max);
    98.     }
    99. }
    100.  
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If one works and the other doesn't, the only thing I can think of is to check the dead zone maybe?

    If you put the one that spins out of control in use. Then write a script to get the axis, even on an empty game object, what does it print out (and does that line up with what you'd imagine)? Maybe compare the 2 types of controllers?
    Otherwise, I'm not sure.