Search Unity

Reverse action

Discussion in 'Scripting' started by PaxStyle, Jul 31, 2014.

  1. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    I guys, this part of code move an arm up when Z is pressed, how can I do for move at the same time the arm down always when I press Z ? So the final result should be that when I press Z the arm should go up and down continuously.

    Thanks!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Braccio : MonoBehaviour {
    5.    
    6.    
    7.     public GameObject[] arm;
    8.    
    9.     public float SpeedUp = 0;
    10.  
    11.     public float acceleration = 10f;
    12.     public float maxSpeed = 100f;
    13.    
    14.     Vector3 StartPosition;
    15.    
    16.    
    17.    
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.        
    22.         SpeedUp = Mathf.Clamp(SpeedUp,0f,maxSpeed); //limit maxspeeed
    23.  
    24.        
    25.         //go up
    26.         if (Input.GetKey (KeyCode.Z)) {
    27.             float currentAngle = transform.rotation.eulerAngles.x;
    28.             while (currentAngle < -180.0f)
    29.                 currentAngle += 360.0f;
    30.             while (currentAngle > 180.0f)
    31.                 currentAngle -= 360.0f;
    32.             Debug.Log ("currentAngle " + currentAngle);
    33.             float destinationAngle = currentAngle - Time.deltaTime * SpeedUp;
    34.             Debug.Log ("destinationAngle before clamp " + destinationAngle);
    35.             destinationAngle = Mathf.Clamp (destinationAngle, -50.0f, 0.0f);
    36.             Debug.Log ("destinationAngle after clamp " + destinationAngle);
    37.             float deltaAngle = destinationAngle - currentAngle;
    38.             transform.Rotate(Vector3.right, deltaAngle);
    39.             SpeedUp += acceleration; //increase speedup
    40.            
    41.            
    42.         }
    43.        
    44.  
    45.        
    46.         else {
    47.            
    48.             SpeedUp -= acceleration; //slowly stop?
    49.  
    50.          }
    51.        
    52.     }
    53. }
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
  3. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    In my case I have transform.rotation, in the example ther's transform.position. Is the same?
    following the example, i made so:
    Code (CSharp):
    1.  transform.Rotate(Vector3.right(Mathf.PingPong(Time.time, 3), deltaAngle);
    but unity give me error.
     
  4. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
  5. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    If you open 3 bracket , you have to close all tree too.

    My guess the error was:
    That means that the compiler does not know what to do witht he ; because he is expeting the ) before the line can end.
     
  6. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    maybe I lost some piece when i copied the code. Anyway, my problem is not that. Hope someone can help me.. LoL
     
  7. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    If you are not more specific, no one can help you.
     
    Macaberz likes this.
  8. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    My question was how can I get the opposite movement from that script. The final result should be that if I press Z, the arm should go up and down continuously, while in that script the arm go only up. I know how make the down movement, need just put float destinationAngle = currentAngle + Time.deltaTime * SpeedDown; in another input, but this is the problem. I would like insert the down movemtent in the same input key (Z)
    Hope you understand me ;)
     
  9. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    AS in you press and then it keeps going up and down till you press again?
    If so you only need a bool to check whether it should be doing that or not, and then make it go up as high as you need til it reaches some condition andthen make it move down and do it all over again.