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.

REQ - Limiting Jamie's rotate with keyboard script

Discussion in 'Scripting' started by DaveyJJ, Jul 15, 2005.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Jamie created a cool little .js script that rotates an object with keyboard input. It goes like this ...

    Code (csharp):
    1. /// This behavior script rotates an object by the mouse delta
    2.  
    3. // All public variables are visible in the inspector and can be edited there.
    4. var speed = 0.1;
    5.  
    6. function Update () {
    7.     x= Input.GetAxis ("Horizontal") * speed;
    8.     y= Input.GetAxis ("Vertical") * speed;
    9.  
    10.     // Rotate around works in radians
    11.     transform.RotateAround (Vector3.fwd, -x);
    12.     transform.RotateAround (Vector3.left, -y);
    13.  
    14. }
    How would I limit the rotation to only 5? (five degrees) up or down in the two planes?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (csharp):
    1.  
    2.  
    3. /// This behavior script rotates an object by the mouse delta
    4.  
    5. // All public variables are visible in the inspector and can be edited there.
    6. var speed = 10;
    7. var x = 0.0;
    8. var y = 0.0;
    9.  
    10. function Update () {
    11. † †x += Input.GetAxis ("Horizontal") * speed;
    12. † †y += Input.GetAxis ("Vertical") * speed;
    13.  
    14.    x = Mathf.Clamp (x, -90.0, 90.0);
    15.    y = Mathf.Clamp (y, -10.0, 10.0);
    16.  
    17.     transform.rotation = Quaternion.identity;
    18.  
    19. † † transform.Rotate (y, x, 0);
    20.  
    21.     // Use the following two lines instead of transform.Rotate (y, x, 0); if you
    22.     //want a fps style rotation
    23.     // transform.Rotate (0, x, 0, Space.World);
    24.     // transform.Rotate (y, 0, 0);
    25. }
    26.  
    In the standard assets there is also a script which does that (MouseLook.cs) but it is written in C# unfortunately.
     
  3. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Thanks much Joe! Each time I get someone's help on the scripting end, I actually figure out more and more of the language, syntax etc.
     
  4. jamie

    jamie

    Joined:
    May 25, 2005
    Posts:
    178
    Amazing! I was about ready to ask the same question! :)

    Thanks for the solution as well...
     
  5. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    I tweaked this to affect the y and z axis, not the x axis (so I simulate pitch and roll but not yaw). Cool! Now I'm trying to, if an object falls below a y value of 35, destroy it and the create it again at a certain world position (for sake of argument lets say x35, y45, z55). Is this somewhat along the right path?

    Code (csharp):
    1. /// a script that will reinstatiate an object if it falls off the platform
    2.  
    3. function Update () {
    4.     if (transform.position.y < -35) {
    5.         Destroy (gameObject);
    6.         transform.Vector3 position (x, y, z);
    7.     }  
    8. }
    I think the transform.Vector3 position (x, y, z); is wrong.
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (csharp):
    1.  
    2. function Update () {
    3. † †if (transform.position.y < -35) {
    4. † †† †transform.position = Vector3 (5, 10, 1);
    5. † †}† †
    6. }
    7.  
    Why do you want to recreate it?
    If you want to recreate it, you have to use the Instantiate function but that probably isn't what you want because:

    Instantiate (gameObject);
    Destroy (gameObject);

    Is the same as doing nothing basically, because you first clone the object, then delete it. So you end up with the same number of objects.
     
  7. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Yes, I thought this after posting it but got busy last night. Rather than destroy and recreate I did mean just to move it to another location.
     
  8. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    If you don't want to manually type the coordinates into the script, you could save the object's initial position at the start of the scene. I'm using something similar to this in my marble game:

    Code (csharp):
    1.  
    2. var respawnPosition: Vector3;
    3.  
    4. function Awake() {
    5.    // save original position
    6.    respawnPosition=transform.position;
    7. }
    8.  
    9. function Update () {
    10.    if (transform.position.y < -35) {
    11.       transform.position = respawnPosition;
    12.    }    
    13. }
    14.