Search Unity

if Statement with consecutive

Discussion in 'Scripting' started by Ivanvan12345, Oct 13, 2015.

  1. Ivanvan12345

    Ivanvan12345

    Joined:
    Sep 12, 2014
    Posts:
    17
    Hello,

    Is there a way to use consecutive numbers in an if statement?

    Here is my script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class rotateBox : MonoBehaviour
    5. {
    6.    
    7.  
    8.         private float degree;
    9.         private float angle;
    10.         public bool MouseActive = true;
    11.         public gamemanager points;
    12.  
    13.         // Use this for initialization
    14.         void Start ()
    15.         {
    16.                 MouseActive = true;
    17.        
    18.         }
    19.  
    20.         void Update ()
    21.         {
    22.                 if (Input.GetMouseButtonDown (0) && MouseActive == true)
    23.                         degree -= 90f;
    24.  
    25.                 angle = Mathf.LerpAngle (transform.rotation.z, degree, 1f * Time.deltaTime);
    26.                 transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, 0, degree), 0.2f);//Time.deltaTime);
    27.         }
    28. }
    So every 30th number the box will change its degree to "+=90f"

    So from 0 to 30 it must be -=90f.
    From 31 to 60 it must be +=90f.
    From 61 to 90 it must be -=90f. And so on endlessly.

    Is there a way to add something like this after the true in the if statement?

    Thank you.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. if(number % 30 ==0)
    3. {
    4.     myValue *= -1;
    5. }
    6.  
     
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    modulo division using the %.
    value % multiple

    if your value is of the multiple you used, it will return 0.

    it more or less does what you would have called remainder division back in early school, and gives you the remainder.
     
  4. Ivanvan12345

    Ivanvan12345

    Joined:
    Sep 12, 2014
    Posts:
    17
    I dont actually understand how to implement this in my code. what is the "myValue *= -1;"?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    number is whatever you are referring to in your description (you don't actually have anything in your code which equates to this...)

    myvalue would be a variable initialised to 90f to start with
     
  6. Ivanvan12345

    Ivanvan12345

    Joined:
    Sep 12, 2014
    Posts:
    17
    So I can't use the points.point variable from another script?
     
  7. Ivanvan12345

    Ivanvan12345

    Joined:
    Sep 12, 2014
    Posts:
    17
    The variable (point) is from another script called gamemanager.

    So would it be:
    Code (CSharp):
    1. if(points.point % 30 == 0){
    2.       //then what?
    3. }