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.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Need help with term search and feasibility questions.

Discussion in 'Getting Started' started by Amuscaria, Aug 24, 2017.

  1. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Sorry, my first chance to catch up.

    You're using transform.Rotate incorrectly. It applies a rotation to it's current rotation. You need something like this.

    Code (CSharp):
    1.  
    2. public float speed = 0.75f;
    3. public float probeAngle = 0f;
    4.  
    5. void Update() {
    6.  
    7. if (Input.GetKey(KeyCode.KeyCode.W));
    8. probeAngle = probeAngle + speed;
    9.  
    10. if (Input.GetKey(KeyCode.KeyCode.S));
    11. probeAngle = probeAngle - speed;
    12.  
    13. probeAngle = Mathf.Clamp(probeAngle, -39, 39);
    14. probe.transform.rotation = Quaternion.Euler(0,0,probeAngle);
    15. }
    Again, beware untested!

    HTH
     
  2. Amuscaria

    Amuscaria

    Joined:
    May 30, 2017
    Posts:
    55
    Ah yes, Mathf-clamping. I was just starting to look it up after reading some some threads on the forums. Thanks for this. I will give this a try and get back to you.Thanks! :)
     
  3. Amuscaria

    Amuscaria

    Joined:
    May 30, 2017
    Posts:
    55
    Having a problem with the Mathf.clamp method. The probe is parented to an invisible object that aligns it to the normal of the skin-mesh. Using Mathclamp seems to outright ignore the parent. I've changed to this code instead, which gives the function I need.

    Code (csharp):
    1.  
    2. void TiltFunction1B () //tilts the probe along Z-axis.
    3.     {
    4.         if (Input.GetKey("w"))
    5.         {
    6.             if (transform.localRotation.z < 0.35)
    7.             {
    8.                 transform.Rotate(0,0,tiltSpeed);
    9.             }
    10.             else
    11.             {
    12.                 transform.Rotate(0,0,0);
    13.             }
    14.         }
    15.         if (Input.GetKey("s"))
    16.         {
    17.             if (transform.localRotation.z  > -0.35)
    18.             {
    19.                 transform.Rotate(0,0,-tiltSpeed);
    20.             }
    21.             else
    22.             {
    23.                 transform.Rotate(0,0,0);
    24.             }
    25.         }
    26.         //comment out when not using. Lags game.
    27.         //Debug.Log("Local parent rotation is " + transform.parent.rotation.z);
    28.         //Debug.Log("Local probe rotation is " + transform.localRotation.z);
    29.     }
    30.  
    The only reason I'm doing this is mostly visual; to stop the probe from rotating into the skin, which looks bad. It doesn't eliminate all clipping, since the program doesn't have realistic softbody physics or anything. Still trying to make it look more believable with just raycasts doing the deformation.
     
    Last edited: Sep 26, 2017
  4. Amuscaria

    Amuscaria

    Joined:
    May 30, 2017
    Posts:
    55
    Ran into an unexpected problem with the angle-limiting functions. The tilt-limits around the Z and X axes work fine, so long as I don't turn the object on its Y-axis 180 degrees. Otherwise, they stop working altogether, and I'm back to the unlimited rotations until I reset the rotation to the parent (with space bar). I can't figure out why that is at all. Here's the complete code:

    Code (csharp):
    1.  
    2.  
    3. public class TiltRotate2 : MonoBehaviour {
    4.  
    5.     // Update is called once per frame
    6.     public float rotateSpeed = 1f;
    7.     public float tiltSpeed = 1f;
    8.  
    9.     void FixedUpdate ()
    10.     {
    11.         TiltFunction1B();
    12.         TiltFunction2B();  
    13.         RotateFunctionB();
    14.         ResetFunctionB();
    15.     }
    16.  
    17.     void TiltFunction1B () //tilts the probe along Z-axis.
    18.     {
    19.         if (Input.GetKey("w"))
    20.         {
    21.             if (transform.localRotation.z < 0.18)
    22.             {
    23.                 transform.Rotate(0,0,tiltSpeed);
    24.             }
    25.             else
    26.             {
    27.                 transform.Rotate(0,0,0);
    28.             }
    29.         }
    30.         if (Input.GetKey("s"))
    31.         {
    32.             if (transform.localRotation.z  > -0.18)
    33.             {
    34.                 transform.Rotate(0,0,-tiltSpeed);
    35.             }
    36.             else
    37.             {
    38.                 transform.Rotate(0,0,0);
    39.             }
    40.         }
    41.         //comment out when not using. Lags game.  
    42.         //Debug.Log("Local probe rotation is " + transform.localRotation.z);
    43.     }
    44.  
    45.     void TiltFunction2B () //tilts the probe along X-axis.
    46.     {
    47.         if (Input.GetKey("a"))
    48.         {
    49.             if (transform.localRotation.x < 0.09)
    50.             {
    51.                 transform.Rotate(tiltSpeed,0,0);
    52.             }
    53.             else
    54.             {
    55.                 transform.Rotate(0,0,0);
    56.             }
    57.         }
    58.         if (Input.GetKey("d"))
    59.         {
    60.             if (transform.localRotation.x  > -0.09)
    61.             {
    62.                 transform.Rotate(-tiltSpeed,0,0);
    63.             }
    64.             else
    65.             {
    66.                 transform.Rotate(0,0,0);
    67.             }
    68.         }
    69.         //comment out when not using. Lags game.
    70.         //Debug.Log("Probe rotation is " + transform.localRotation.x);
    71.     }
    72.  
    73.     void RotateFunctionB() //rotates the probe along Y-axis
    74.     {  
    75.         if (Input.GetKey("e"))
    76.         {
    77.             transform.Rotate(0,rotateSpeed,0);
    78.         }
    79.         if (Input.GetKey("q"))
    80.         {
    81.             transform.Rotate(0,-rotateSpeed,0);
    82.         }
    83.         //Debug.Log("Probe rotation is " + transform.localRotation.y);
    84.     }
    85.  
    86.     void ResetFunctionB() //resets the probe to the parent's orientation
    87.     {
    88.         if (Input.GetKey("space"))
    89.         {
    90.             transform.rotation=transform.parent.rotation; //works but instant
    91.         }
    92.     }
    93. }
    94.  
    95.  
     
  5. Amuscaria

    Amuscaria

    Joined:
    May 30, 2017
    Posts:
    55
    EDIT: Nvm. Just figured out the problem. The conditions don't apply when the X-axis is rotated 180 degrees, because the parent didn't rotate with it.

    EDIT2: Problem fixed by adding an additional parent, and seperating the tilt and rotate functions between the new parent and the probe. :)
     
    Last edited: Sep 27, 2017