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. Dismiss Notice

Creating a Smoothstep rotation in a script

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Apr 12, 2016.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am trying to avoid animations where possible as both animating in Unity and porting animations over to it can be tricky and I am no animator, so I decided programming the easier stuff would be best, and easier for re-use later on.

    I am trying to put together a script that takes 2 transforms or Vector3s to input the starting position and ending position of the position, rotation or scale depending on which is selected, and have a boolean to toggle whether the action is toggle-able in-game or not (whether you can close it once opened), but this is proving to be a lot harder than I thought.

    I was under the impression that position, rotation and scale were all float's in a Vector3 formation, this doesn't appear to be true as the people at Unity have taken a very confusing stance to using EulerAngles for rotation, which means (if I understand correctly) that I cannot use a Lerp as these are for float values, not EulerAngles.

    I have tried several things that haven't worked over the past hour and can't seem to find much else that isn't related to things I have already tried and failed with.

    I have posted the shell of my script (the bit without the working code) below this post, hopefully someone knows how to do these things.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public enum OPTIONS
    5. {
    6.     Transform = 0,
    7.     Rotate = 1,
    8.     Scale = 2
    9. }
    10.  
    11. public class AnimationInteract : MonoBehaviour
    12. {
    13.  
    14.     public AudioClip soundToPlay;
    15.     public GameObject interactObject;
    16.  
    17.     public Vector3 targetAngle = new Vector3(0f, 0f, 0f);
    18.  
    19.     private Vector3 currentAngle;
    20.  
    21.     public OPTIONS method;
    22.  
    23.     public bool smooth;
    24.     public bool toggle;
    25.  
    26.     protected void Start()
    27.     {
    28.         currentAngle = interactObject.transform.eulerAngles;
    29.     }
    30.  
    31.     void OnTriggerEnter(Collider col)
    32.     {
    33.  
    34.         if (col.gameObject.tag == "Player")
    35.         {
    36.             switch (method)
    37.             {
    38.                 case OPTIONS.Transform:
    39.                     if(!smooth)
    40.                     {
    41.                         MoveObject();
    42.                     }
    43.                     if(smooth)
    44.                     {
    45.                         MoveObjectSmooth();
    46.                     }
    47.                     break;
    48.                 case OPTIONS.Rotate:
    49.                     if(!smooth)
    50.                     {
    51.                         RotateUponAxis();
    52.                     }
    53.                     if(smooth)
    54.                     {
    55.                         RotateUponAxisSmooth();
    56.                     }
    57.                     break;
    58.                 case OPTIONS.Scale:
    59.                     if(!smooth)
    60.                     {
    61.                         ChangeObjectScale();
    62.                     }
    63.                     if(smooth)
    64.                     {
    65.                         ChangeObjectScaleSmooth();
    66.                     }
    67.                     break;
    68.                 default:
    69.                     Debug.LogError("Unrecognized Option");
    70.                     break;
    71.             }
    72.         }
    73.     }
    74.  
    75.     public void MoveObject()
    76.     {
    77.  
    78.     }
    79.  
    80.     public void MoveObjectSmooth()
    81.     {
    82.  
    83.     }
    84.  
    85.     public void RotateUponAxis()
    86.     {
    87.  
    88.     }
    89.  
    90.     public void RotateUponAxisSmooth()
    91.     {
    92.  
    93.     }
    94.  
    95.     public void ChangeObjectScale()
    96.     {
    97.  
    98.     }
    99.  
    100.     public void ChangeObjectScaleSmooth()
    101.     {
    102.  
    103.     }
    104. }
     
  2. Trexug

    Trexug

    Joined:
    Dec 2, 2013
    Posts:
    88
    Only in the inspector. Transform.rotation is a quaternion. You should have a look at Quaternion.Lerp/Quaternion.Slerp, as it is very likely what you are looking for.
     
    Last edited: Apr 12, 2016