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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

smooth move & rotate object to set position and rotation.

Discussion in 'Scripting' started by jamuk, Aug 12, 2014.

  1. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    I've been working on the following script (am new to scripting).
    I am able to mouse click on object enabling it to spin and slow down over time. What i would like to do is add the ability to press a button and have it move to a set position and rotation from where its position currently is after coming to a stop.
    I have managed to get the translation working but am struggling with the rotation. I've read so many forums on it that now i am completely confused as to what I'm supposed to use. Is it vector3 or Quaternion or what. Someone please help!!!

    #pragmastrict

    //staticvarmain:Camera;
    varrotationSpeed = 5.0;
    varlerpSpeed = 1.0;

    //positionvar's
    publicvarsmooth : float;
    privatevarnewPosition : Vector3;
    //Endpositionvar's

    privatevarspeed = newVector3();
    privatevaravgSpeed = newVector3();
    privatevardragging = false;
    privatevartargetSpeedX = newVector3();

    //GUIstyle
    varcustomGuiStyle : GUIStyle;

    //awakefunction
    functionAwake ()
    {
    newPosition = transform.position;

    }
    //Endawakefunction

    functionOnMouseDown()
    {
    dragging = true;
    }


    functionUpdate ()
    {


    if (Input.GetMouseButton(0) && dragging) {
    speed = newVector3(-Input.GetAxis ("MouseX"), Input.GetAxis("MouseY"), 0);
    avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
    } else {
    if (dragging) {
    speed = avgSpeed;
    dragging = false;
    }
    vari = Time.deltaTime * lerpSpeed;
    speed = Vector3.Lerp( speed, Vector3.zero, i);
    }

    transform.Rotate( Camera.main.transform.up * speed.x * rotationSpeed, Space.World );
    transform.Rotate( Camera.main.transform.right * speed.y * rotationSpeed, Space.World );

    }


    functionOnGUI() {


    //RotationTranslationPositions//
    varpositionReset : Vector3 = newVector3(0, 0, 0);
    varpositionA : Vector3 = newVector3(-20, 3, 20);
    varpositionB : Vector3 = newVector3(30, 3, 10);
    //EndRotationTranslationPositions//

    transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);


    if (GUILayout.Button ("reset", customGuiStyle)) {
    newPosition = positionReset;
    transform.rotation = Quaternion.identity;

    }
    if (GUI.Button(Rect(0,350,100,50), "PositionA")) {
    newPosition = positionA;
    }
    if (GUI.Button(Rect(0,400,100,50), "PositionB")) {
    newPosition = positionB;
    }
    }
     
  2. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
  3. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    thanks Fraconte, I will check it out - although i notice its written in C sharp and I'm just learning JavaScript, will see if i can get my head around it.
     
  4. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
  5. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Wow that worked awesome, many thanks! I have hit another small problem that maybe you could help me with.

    transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, smooth * Time.deltaTime);

    I only want this part of the code to run when the object spinning (dragging) is false as it seems to be fighting against my spin object code. ( starts rotating the object back on mouse release) So thought would add it to my else if (dragging) statement which does work but only moves a bit at a time on multiple button presses.

    Could you point me in the right direction again, thanks in advance.

    Script so far:

    #pragmastrict

    //staticvarmain:Camera;
    varrotationSpeed = 5.0;
    varlerpSpeed = 1.0;

    //positionvar's
    publicvarsmooth : float;
    privatevarnewPosition : Vector3;
    privatevarnewRotation : Quaternion;
    //Endpositionvar's

    privatevarspeed = newVector3();
    privatevaravgSpeed = newVector3();
    privatevardragging = false;
    privatevartargetSpeedX = newVector3();

    //GUIstyle
    varcustomGuiStyle : GUIStyle;


    functionAwake ()
    {
    newPosition = transform.position;
    newRotation = transform.rotation;
    }


    functionOnMouseDown()
    {
    dragging = true;
    }

    functionOnGUI()
    {
    //Spinobject
    if (Input.GetMouseButton(0) && dragging) {
    speed = newVector3(-Input.GetAxis ("MouseX"), Input.GetAxis("MouseY"), 0);
    avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);

    } else {
    if (dragging) {
    speed = avgSpeed;
    dragging = false;

    }
    vari = Time.deltaTime * lerpSpeed;
    speed = Vector3.Lerp( speed, Vector3.zero, i);
    }
    transform.Rotate( Camera.main.transform.up * speed.x * rotationSpeed, Space.World );
    transform.Rotate( Camera.main.transform.right * speed.y * rotationSpeed, Space.World );
    //EndSpinObject

    transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, smooth * Time.deltaTime);

    //RotationTranslationPositions//
    varpositionReset : Vector3 = newVector3(0, 0, 0);
    varrotationReset = Quaternion.Euler(Vector3(0, 0, 0));

    varpositionA : Vector3 = newVector3(-20, 3, 20);
    varrotationA = Quaternion.Euler(Vector3(180, 0, 0));

    varpositionB : Vector3 = newVector3(30, 3, 10);
    varrotationB = Quaternion.Euler(Vector3(0, 0, 180));

    varpositionC : Vector3 = newVector3(22, 12, 12);
    varrotationC = Quaternion.Euler(Vector3(0, 270, 0));
    //EndRotationTranslationPositions//


    //GUIButtons//
    if (GUILayout.Button ("reset", customGuiStyle)) {
    newPosition = positionReset;
    newRotation = rotationReset;
    }

    if (GUI.Button(Rect(0,350,100,50), "PositionA")) {
    newPosition = positionA;
    newRotation = rotationA;
    }
    if (GUI.Button(Rect(0,400,100,50), "PositionB")) {
    newPosition = positionB;
    newRotation = rotationB;
    }

    if (GUILayout.Button ("panUp", customGuiStyle)) {
    newPosition = positionC;
    newRotation = rotationC;
    }
    //EndGUIButtons//

    }
     
  6. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Use the Insert Code button to make your code readable... and tell me how is the scene setup (what object have what script etc.)
     
  7. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    just one object with this script attached and some static spot lights. The scene is a simple model viewer, you can rotate the object with the mouse or press buttons to view different angles via the GUI, Thanks jamuk.



    Code (JavaScript):
    1.  
    2. //static var main:Camera;
    3. var rotationSpeed = 5.0;
    4. var lerpSpeed = 1.0;
    5.  
    6. //position var's
    7. public var smooth : float;
    8. private var newPosition : Vector3;
    9. private var newRotation : Quaternion;
    10. //End position var's
    11.  
    12. private var speed = new Vector3();
    13. private var avgSpeed = new Vector3();
    14. private var dragging = false;
    15. private var targetSpeedX = new Vector3();
    16.  
    17. //GUI style
    18. var customGuiStyle : GUIStyle;
    19.  
    20.  
    21. function Awake ()
    22. {
    23.     newPosition = transform.position;
    24.     newRotation = transform.rotation;
    25.    
    26. }
    27.  
    28. function OnMouseDown()
    29. {
    30. dragging = true;
    31.  
    32. }
    33. function Update ()
    34. {
    35. }
    36.  
    37.  
    38.     function OnGUI()
    39. {
    40.    //Spin object
    41.    if (Input.GetMouseButton(0) && dragging) {
    42.    speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
    43.    avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
    44.  
    45.    } else {
    46.    if (dragging) {
    47.    speed = avgSpeed;
    48.    dragging = false;
    49.  
    50. }
    51.    var i = Time.deltaTime * lerpSpeed;
    52.    speed = Vector3.Lerp( speed, Vector3.zero, i);
    53. }
    54.    transform.Rotate( Camera.main.transform.up * speed.x * rotationSpeed, Space.World );
    55.    transform.Rotate( Camera.main.transform.right * speed.y * rotationSpeed, Space.World );
    56.    //End Spin Object
    57.  
    58.    transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    59.    transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, smooth * Time.deltaTime);
    60.    
    61.    
    62.     //Rotation Translation Positions//
    63.       var positionReset : Vector3 = new Vector3(0, 0, 0);
    64.       var rotationReset = Quaternion.Euler(Vector3(0, 0, 0));
    65.      
    66.       var positionA : Vector3 = new Vector3(-20, 3, 20);
    67.       var rotationA = Quaternion.Euler(Vector3(180, 0, 0));
    68.      
    69.       var positionB : Vector3 = new Vector3(30, 3, 10);
    70.       var rotationB = Quaternion.Euler(Vector3(0, 0, 180));
    71.      
    72.       var positionC : Vector3 = new Vector3(22, 12, 12);
    73.       var rotationC = Quaternion.Euler(Vector3(0, 270, 0));
    74.     //End Rotation Translation Positions//
    75.    
    76.  
    77.     //GUI Buttons//
    78.     if (GUILayout.Button ("reset", customGuiStyle)) {
    79.         newPosition = positionReset;
    80.         newRotation = rotationReset;
    81.     }
    82.  
    83.     if (GUI.Button(Rect(0,350,100,50), "PositionA")) {
    84.         newPosition = positionA;
    85.         newRotation = rotationA;
    86.     }
    87.     if (GUI.Button(Rect(0,400,100,50), "PositionB")) {
    88.         newPosition = positionB;
    89.         newRotation = rotationB;
    90.     }
    91.    
    92.     if (GUILayout.Button ("panUp", customGuiStyle)) {
    93.         newPosition = positionC;
    94.         newRotation = rotationC;
    95.     }
    96.     //End GUI Buttons//
    97.    
    98.    }
     
  8. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Of course they interfere... Put them in a function and call it only when you press the buttons:

    Code (JavaScript):
    1.  
    2. function MoveRotate (newPosition : Vector3, newRotation : Quaternion)
    3. {
    4.      transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    5.      transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, smooth * Time.deltaTime);
    6. }
    7.  
    Edit: var error corrected
     
    Last edited: Aug 12, 2014
  9. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Thanks Fraconte - I added the function and MoveRotate(); to my button to call it but i keep getting an error BCE0043 Unexpected token var
     
  10. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. //static var main:Camera;
    4. var rotationSpeed = 5.0;
    5. var lerpSpeed = 1.0;
    6.  
    7. //position var's
    8. public var smooth : float;
    9. private var newPosition : Vector3;
    10. private var newRotation : Quaternion;
    11. //End position var's
    12.  
    13. private var speed = new Vector3();
    14. private var avgSpeed = new Vector3();
    15. private var dragging = false;
    16. private var targetSpeedX = new Vector3();
    17.  
    18. //GUI style
    19. var mainSkin : GUISkin;
    20. var customGuiStyle : GUIStyle;
    21.  
    22.  
    23. function Awake ()
    24. {
    25.     newPosition = transform.position;
    26.     newRotation = transform.rotation;
    27.    
    28. }
    29.  
    30. function moveRotate (var newPosition : Vector3, var newRotation : Quaternion)
    31.    {
    32.         transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    33.         transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, smooth * Time.deltaTime);
    34.    }
    35.  
    36. function OnMouseDown()
    37. {
    38. dragging = true;
    39.  
    40. }
    41.  
    42.  
    43.  
    44.     function OnGUI()
    45. {
    46.  
    47. GUI.skin = mainSkin;
    48.    //Spin object
    49.    if (Input.GetMouseButton(0) && dragging) {
    50.    speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
    51.    avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
    52.  
    53.    } else {
    54.    if (dragging) {
    55.    speed = avgSpeed;
    56.    dragging = false;
    57.  
    58. }
    59.    var i = Time.deltaTime * lerpSpeed;
    60.    speed = Vector3.Lerp( speed, Vector3.zero, i);
    61. }
    62.    transform.Rotate( Camera.main.transform.up * speed.x * rotationSpeed, Space.World );
    63.    transform.Rotate( Camera.main.transform.right * speed.y * rotationSpeed, Space.World );
    64.    //End Spin Object
    65.  
    66.  
    67.    
    68.    
    69.     //Rotation Translation Positions//
    70.       var positionReset : Vector3 = new Vector3(0, 0, 0);
    71.       var rotationReset = Quaternion.Euler(Vector3(0, 0, 0));
    72.      
    73.       var positionA : Vector3 = new Vector3(-20, 3, 20);
    74.       var rotationA = Quaternion.Euler(Vector3(180, 0, 0));
    75.      
    76.       var positionB : Vector3 = new Vector3(30, 3, 10);
    77.       var rotationB = Quaternion.Euler(Vector3(0, 0, 180));
    78.      
    79.       var positionC : Vector3 = new Vector3(22, 12, 12);
    80.       var rotationC = Quaternion.Euler(Vector3(0, 270, 0));
    81.     //End Rotation Translation Positions//
    82.    
    83.  
    84.     //GUI Buttons//
    85.    
    86.     GUI.Box(Rect(50,50,400,400), "Controls");
    87.        
    88.     if (GUILayout.Button ("reset", customGuiStyle)) {
    89.         newPosition = positionReset;
    90.         newRotation = rotationReset;
    91.         moveRotate();
    92.     }
    93.  
    94.     if (GUI.Button(Rect(0,350,100,50), "PositionA")) {
    95.         newPosition = positionA;
    96.         newRotation = rotationA;
    97.         moveRotate();
    98.     }
    99.    
    100.     if (GUI.Button(Rect(Screen.width/2 - 75,Screen.height/2 - 75,100,50), "PositionB")) {
    101.         newPosition = positionB;
    102.         newRotation = rotationB;
    103.         moveRotate();
    104.     }
    105.    
    106.     if (GUILayout.Button ("panUp", customGuiStyle)) {
    107.         newPosition = positionC;
    108.         newRotation = rotationC;
    109.         moveRotate();
    110.     }
    111.    
    112.  
    113.     //End GUI Buttons//
    114.    
    115.    }
    116.    
     
  11. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Sorry there was a var in excess in the function's arguments. I corrected the code.
     
  12. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Can I see the code?:(
     
  13. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    I edited my post up there. :)
     
  14. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    I'm sorry I wasn't paying attention.
    To call the function do I put MoveRotate(); on my button ?
    I get this error but cant work it out.
    BCE0017: The best overload for the method 'rotateObject.MoveRotate(UnityEngine.Vector3, UnityEngine.Quaternion)' is not compatible with the argument list '()'.
     
  15. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    You have to pass 2 parameters to MoveRotate... But you can simply get rid of the parameters in the function definition (you just have newPosition and newRotation as private var).

    Change
    function MoveRotate (newPosition : Vector3, newRotation : Quaternion)
    in
    function MoveRotate ()
     
  16. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Thanks Fraconte.
    I'm still getting the same problem where the object moves to its new position one frame at a time on each button press.
    Apologies if I'm being really stupid this is all quite new to me.


    Code (CSharp):
    1. #pragma strict
    2.  
    3. //static var main:Camera;
    4. var rotationSpeed = 5.0;
    5. var lerpSpeed = 1.0;
    6.  
    7. //position var's
    8. public var smooth : float;
    9. private var newPosition : Vector3;
    10. private var newRotation : Quaternion;
    11. //End position var's
    12.  
    13. private var speed = new Vector3();
    14. private var avgSpeed = new Vector3();
    15. private var dragging = false;
    16. private var targetSpeedX = new Vector3();
    17.  
    18. //GUI Style
    19. var customSkin : GUISkin;
    20. //Button Icons//
    21. var btnReset : Texture;
    22. var btnPanLt : Texture;
    23. var btnPanRt : Texture;
    24. var btnPanUp : Texture;
    25. var btnPanDown : Texture;
    26. var btnRotLt : Texture;
    27. var btnRotRt : Texture;
    28. var btnRotUp : Texture;
    29. var btnRotDown : Texture;
    30. var btnZoomIn : Texture;
    31. var btnZoomOut : Texture;
    32. //End Button Icons//
    33.  
    34. function Awake ()
    35. {
    36.     newPosition = transform.position;
    37.     newRotation = transform.rotation;
    38.    
    39. }
    40. function OnMouseDown()
    41. {
    42. dragging = true;
    43.  
    44. }
    45.  
    46. function MoveRotate ()
    47. {
    48.      transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    49.      transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, smooth * Time.deltaTime);
    50. }
    51.  
    52.  
    53.     function OnGUI()
    54. {
    55.  
    56.    //Apply custom GUI skin//
    57.    GUI.skin = customSkin;
    58.  
    59.  
    60.    //Spin object
    61.    if (Input.GetMouseButton(0) && dragging) {
    62.    speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
    63.    avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
    64.  
    65.    } else {
    66.    if (dragging) {
    67.    speed = avgSpeed;
    68.    dragging = false;
    69.  
    70. }
    71.    var i = Time.deltaTime * lerpSpeed;
    72.    speed = Vector3.Lerp( speed, Vector3.zero, i);
    73. }
    74.    transform.Rotate( Camera.main.transform.up * speed.x * rotationSpeed, Space.World );
    75.    transform.Rotate( Camera.main.transform.right * speed.y * rotationSpeed, Space.World );
    76.    //End Spin Object
    77.  
    78.    
    79.    
    80.     //Rotation Translation Positions//
    81.       var positionReset : Vector3 = new Vector3(0, 0, 0);
    82.       var rotationReset = Quaternion.Euler(Vector3(0, 0, 0));
    83.      
    84.       var positionA : Vector3 = new Vector3(-20, 3, 20);
    85.       var rotationA = Quaternion.Euler(Vector3(180, 0, 0));
    86.      
    87.       var positionB : Vector3 = new Vector3(30, 3, 10);
    88.       var rotationB = Quaternion.Euler(Vector3(0, 0, 180));
    89.      
    90.       var positionC : Vector3 = new Vector3(22, 12, 12);
    91.       var rotationC = Quaternion.Euler(Vector3(0, 270, 0));
    92.     //End Rotation Translation Positions//
    93.    
    94.  
    95.     //GUI Buttons//
    96.  
    97.    
    98.     // Make a group on the center of the screen
    99.     GUI.BeginGroup (Rect ((Screen.width / 2) - Screen.width / 4, (Screen.height / 2) - Screen.height / 4, Screen.width / 2, Screen.height / 2));
    100.     //GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 500, 60));
    101.     // All rectangles are now adjusted to the group. (0,0) is the topleft corner of the group.
    102.     GUI.Box (Rect (0,0,400,60), "delete when done");
    103.    
    104.        
    105.     //Control BTNs//              
    106.         if (GUI.Button(Rect(250,15,22,22), btnReset)) {
    107.         newPosition = positionReset;
    108.         newRotation = rotationReset;
    109.         MoveRotate();
    110.     }
    111.    
    112.         if (GUI.Button(Rect(60,15,22,22), btnPanLt)) {
    113.  
    114.          
    115.     }
    116.         if (GUI.Button(Rect(90,15,22,22), btnPanRt)) {
    117.  
    118.          
    119.     }
    120.         if (GUI.Button(Rect(75,0,22,22), btnPanUp)) {
    121.  
    122.          
    123.     }
    124.         if (GUI.Button(Rect(75,30,22,22), btnPanDown)) {
    125.  
    126.          
    127.     }
    128.         if (GUI.Button(Rect(160,15,22,22), btnRotLt)) {
    129.  
    130.          
    131.     }
    132.         if (GUI.Button(Rect(130,15,22,22), btnRotRt)) {
    133.  
    134.          
    135.     }
    136.         if (GUI.Button(Rect(145,30,22,22), btnRotUp)) {
    137.  
    138.          
    139.     }
    140.         if (GUI.Button(Rect(145,0,22,22), btnRotDown)) {
    141.  
    142.          
    143.     }
    144.         if (GUI.Button(Rect(200,0,22,22), btnZoomIn)) {
    145.  
    146.          
    147.     }
    148.         if (GUI.Button(Rect(200,30,22,22), btnZoomOut)) {
    149.  
    150.          
    151.     }
    152.     //END Control BTNs//
    153.     GUI.EndGroup();
    154.     //Animation BTNs//
    155.  
    156.    
    157.     if (GUI.Button(Rect(290,15,22,22), "A")) {
    158.         newPosition = positionA;
    159.         newRotation = rotationA;
    160.         MoveRotate();
    161.     }
    162.    
    163.     if (GUI.Button(Rect(320,15,22,22), "B")) {
    164.         newPosition = positionB;
    165.         newRotation = rotationB;
    166.         MoveRotate();
    167.     }
    168.    
    169.     if (GUI.Button(Rect(3,15,22,22), "C")) {
    170.         newPosition = positionC;
    171.         newRotation = rotationC;
    172.         MoveRotate();
    173.     }
    174.    
    175.     //End Animation BTNs//
    176.    
    177.  
    178.  
    179.     //End GUI Buttons//
    180.    
    181.    }
    182.    
     
  17. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    My fault sorry... moveUpdate have to be called from Update when you press a button. The problem is you know when to start moving but you have to find a way to stop too.
     
  18. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Try this:
    Code (JavaScript):
    1.  
    2. function Update ()
    3. {
    4.     if (!dragging)
    5.         MoveRotate ();
    6. }
    7.  
    and in the buttons code simply set dragging = false;
     
  19. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Thanks for all your help on this.
    I took out the function awake() so now when it starts the spinning works great. When i hit the buttons to change its position and rotation it works, however when i go to spin the object again after button press it fights against the spin code and try's to move back to the target location again.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. //static var main:Camera;
    4. var rotationSpeed = 5.0;
    5. var lerpSpeed = 1.0;
    6.  
    7. //position var's
    8. public var smooth : float;
    9. private var newPosition : Vector3;
    10. private var newRotation : Quaternion;
    11. //End position var's
    12.  
    13. private var speed = new Vector3();
    14. private var avgSpeed = new Vector3();
    15. private var dragging = false;
    16. private var targetSpeedX = new Vector3();
    17.  
    18. //GUI Style
    19. var customSkin : GUISkin;
    20. //Button Icons//
    21. var btnReset : Texture;
    22. var btnPanLt : Texture;
    23. var btnPanRt : Texture;
    24. var btnPanUp : Texture;
    25. var btnPanDown : Texture;
    26. var btnRotLt : Texture;
    27. var btnRotRt : Texture;
    28. var btnRotUp : Texture;
    29. var btnRotDown : Texture;
    30. var btnZoomIn : Texture;
    31. var btnZoomOut : Texture;
    32. //End Button Icons//
    33.  
    34.  
    35.  
    36. function MoveRotate ()
    37. {
    38.  
    39.      transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    40.      transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, smooth * Time.deltaTime);
    41.    
    42. }
    43.  
    44. function Update ()
    45. {
    46.  
    47.     if (!dragging)
    48.         MoveRotate ();
    49.        
    50.        
    51.    //Spin object
    52.    if (Input.GetMouseButton(0) && dragging) {
    53.    speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
    54.    avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
    55.  
    56.    } else {
    57.    if (dragging) {
    58.    speed = avgSpeed;
    59.    dragging = false;
    60.  
    61. }
    62.    var i = Time.deltaTime * lerpSpeed;
    63.    speed = Vector3.Lerp( speed, Vector3.zero, i);
    64. }
    65.    transform.Rotate( Camera.main.transform.up * speed.x * rotationSpeed, Space.World );
    66.    transform.Rotate( Camera.main.transform.right * speed.y * rotationSpeed, Space.World );
    67.    //End Spin Object
    68.        
    69. }
    70. function OnMouseDown()
    71. {
    72. dragging = true;
    73.  
    74. }
    75.  
    76.  
    77.     function OnGUI()
    78. {
    79.  
    80.    //Apply custom GUI skin//
    81.    GUI.skin = customSkin;
    82.  
    83.  
    84.  
    85.  
    86.    
    87.    
    88.     //Rotation Translation Positions//
    89.       var positionReset : Vector3 = new Vector3(0, 0, 0);
    90.       var rotationReset = Quaternion.Euler(Vector3(0, 0, 0));
    91.      
    92.       var positionA : Vector3 = new Vector3(-20, 3, 20);
    93.       var rotationA = Quaternion.Euler(Vector3(180, 0, 0));
    94.      
    95.       var positionB : Vector3 = new Vector3(30, 3, 10);
    96.       var rotationB = Quaternion.Euler(Vector3(0, 0, 180));
    97.      
    98.       var positionC : Vector3 = new Vector3(22, 12, 12);
    99.       var rotationC = Quaternion.Euler(Vector3(0, 270, 0));
    100.     //End Rotation Translation Positions//
    101.    
    102.  
    103.     //GUI Buttons//
    104.  
    105.    
    106.     // Make a group on the center of the screen
    107.     GUI.BeginGroup (Rect ((Screen.width / 2) - Screen.width / 4, (Screen.height / 2) - Screen.height / 4, Screen.width / 2, Screen.height / 2));
    108.     //GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 500, 60));
    109.     // All rectangles are now adjusted to the group. (0,0) is the topleft corner of the group.
    110.     GUI.Box (Rect (0,0,400,60), "delete when done");
    111.    
    112.        
    113.     //Control BTNs//              
    114.         if (GUI.Button(Rect(250,15,22,22), btnReset)) {
    115.         newPosition = positionReset;
    116.         newRotation = rotationReset;
    117.         MoveRotate();
    118.         dragging = false;
    119.     }
    120.    
    121.         if (GUI.Button(Rect(60,15,22,22), btnPanLt)) {
    122.  
    123.          
    124.     }
    125.         if (GUI.Button(Rect(90,15,22,22), btnPanRt)) {
    126.  
    127.          
    128.     }
    129.         if (GUI.Button(Rect(75,0,22,22), btnPanUp)) {
    130.  
    131.          
    132.     }
    133.         if (GUI.Button(Rect(75,30,22,22), btnPanDown)) {
    134.  
    135.          
    136.     }
    137.         if (GUI.Button(Rect(160,15,22,22), btnRotLt)) {
    138.  
    139.          
    140.     }
    141.         if (GUI.Button(Rect(130,15,22,22), btnRotRt)) {
    142.  
    143.          
    144.     }
    145.         if (GUI.Button(Rect(145,30,22,22), btnRotUp)) {
    146.  
    147.          
    148.     }
    149.         if (GUI.Button(Rect(145,0,22,22), btnRotDown)) {
    150.  
    151.          
    152.     }
    153.         if (GUI.Button(Rect(200,0,22,22), btnZoomIn)) {
    154.  
    155.          
    156.     }
    157.         if (GUI.Button(Rect(200,30,22,22), btnZoomOut)) {
    158.  
    159.          
    160.     }
    161.     //END Control BTNs//
    162.     GUI.EndGroup();
    163.     //Animation BTNs//
    164.  
    165.    
    166.     if (GUI.Button(Rect(290,15,22,22), "A")) {
    167.         newPosition = positionA;
    168.         newRotation = rotationA;
    169.  
    170.     }
    171.    
    172.     if (GUI.Button(Rect(320,15,22,22), "B")) {
    173.         newPosition = positionB;
    174.         newRotation = rotationB;
    175.        
    176.     }
    177.    
    178.     if (GUI.Button(Rect(3,15,22,22), "C")) {
    179.         newPosition = positionC;
    180.         newRotation = rotationC;
    181.        
    182.     }
    183.    
    184.     //End Animation BTNs//
    185.    
    186.  
    187.  
    188.     //End GUI Buttons//
    189.    
    190.    }
    191.    
     
  20. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Try to set dragging = false ONLY when you press a button.
     
  21. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Hi Fraconte,
    I think I did that but without much luck. I have uploaded a demo to my website as i don't think i am very good at explaining things.

    http://jbayley3d.com/boxTest/boxTest.html

    If you mouse click the cube you can spin it around. At the top of the screen click buttons A,B and C. It will rotate and position the cube, but when you go to click on it and spin again it slowly rotates back to the start position and looks a bit shaky.

    Code (CSharp):
    1. #pragma strict
    2.  
    3. //static var main:Camera;
    4. var rotationSpeed = 5.0;
    5. var lerpSpeed = 1.0;
    6.  
    7. //position var's
    8. public var smooth : float;
    9. private var newPosition : Vector3;
    10. private var newRotation : Quaternion;
    11. //End position var's
    12.  
    13. private var speed = new Vector3();
    14. private var avgSpeed = new Vector3();
    15. private var dragging = false;
    16. private var targetSpeedX = new Vector3();
    17.  
    18. //GUI Style
    19. var customSkin : GUISkin;
    20. //Button Icons//
    21. var btnReset : Texture;
    22. var btnPanLt : Texture;
    23. var btnPanRt : Texture;
    24. var btnPanUp : Texture;
    25. var btnPanDown : Texture;
    26. var btnRotLt : Texture;
    27. var btnRotRt : Texture;
    28. var btnRotUp : Texture;
    29. var btnRotDown : Texture;
    30. var btnZoomIn : Texture;
    31. var btnZoomOut : Texture;
    32. //End Button Icons//
    33.  
    34.  
    35.  
    36. function MoveRotate ()
    37. {
    38.  
    39.      transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    40.      transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, smooth * Time.deltaTime);
    41.    
    42. }
    43.  
    44. function Update ()
    45. {
    46.  
    47.     if (!dragging)
    48.         MoveRotate ();
    49.          
    50.        
    51.    //Spin object
    52.    if (Input.GetMouseButton(0) && dragging) {
    53.    speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
    54.    avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
    55.  
    56.    } else {
    57.    if (dragging) {
    58.    speed = avgSpeed;
    59.    dragging = false;
    60.  
    61. }
    62.    var i = Time.deltaTime * lerpSpeed;
    63.    speed = Vector3.Lerp( speed, Vector3.zero, i);
    64. }
    65.    transform.Rotate( Camera.main.transform.up * speed.x * rotationSpeed, Space.World );
    66.    transform.Rotate( Camera.main.transform.right * speed.y * rotationSpeed, Space.World );
    67.    //End Spin Object
    68.        
    69. }
    70. function OnMouseDown()
    71. {
    72. dragging = true;
    73.  
    74. }
    75.  
    76.  
    77.     function OnGUI()
    78. {
    79.  
    80.    //Apply custom GUI skin//
    81.    GUI.skin = customSkin;
    82.  
    83.  
    84.  
    85.  
    86.    
    87.    
    88.     //Rotation Translation Positions//
    89.       var positionReset : Vector3 = new Vector3(0, 0, 0);
    90.       var rotationReset = Quaternion.Euler(Vector3(0, 0, 0));
    91.      
    92.       var positionA : Vector3 = new Vector3(-20, 3, 20);
    93.       var rotationA = Quaternion.Euler(Vector3(180, 0, 0));
    94.      
    95.       var positionB : Vector3 = new Vector3(30, 3, 10);
    96.       var rotationB = Quaternion.Euler(Vector3(0, 0, 180));
    97.      
    98.       var positionC : Vector3 = new Vector3(22, 12, 12);
    99.       var rotationC = Quaternion.Euler(Vector3(0, 270, 0));
    100.     //End Rotation Translation Positions//
    101.    
    102.  
    103.     //GUI Buttons//
    104.  
    105.    
    106.     // Make a group on the center of the screen
    107.     GUI.BeginGroup (Rect ((Screen.width / 2) - Screen.width / 4, (Screen.height / 2) - Screen.height / 4, Screen.width / 2, Screen.height / 2));
    108.     //GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 500, 60));
    109.     // All rectangles are now adjusted to the group. (0,0) is the topleft corner of the group.
    110.     GUI.Box (Rect (0,0,400,60), "control");
    111.    
    112.        
    113.     //Control BTNs//              
    114.         if (GUI.Button(Rect(250,15,22,22), btnReset)) {
    115.         newPosition = positionReset;
    116.         newRotation = rotationReset;
    117.  
    118.         dragging = false;
    119.     }
    120.    
    121.         if (GUI.Button(Rect(60,15,22,22), btnPanLt)) {
    122.         newRotation = Quaternion.AngleAxis(45.0, Vector3.up) * newRotation;
    123.          
    124.     }
    125.         if (GUI.Button(Rect(90,15,22,22), btnPanRt)) {
    126.         newRotation = Quaternion.AngleAxis(-45.0, Vector3.up) * newRotation;
    127.          
    128.     }
    129.         if (GUI.Button(Rect(75,0,22,22), btnPanUp)) {
    130.  
    131.          
    132.     }
    133.         if (GUI.Button(Rect(75,30,22,22), btnPanDown)) {
    134.  
    135.          
    136.     }
    137.         if (GUI.Button(Rect(160,15,22,22), btnRotLt)) {
    138.  
    139.          
    140.     }
    141.         if (GUI.Button(Rect(130,15,22,22), btnRotRt)) {
    142.  
    143.          
    144.     }
    145.         if (GUI.Button(Rect(145,30,22,22), btnRotUp)) {
    146.  
    147.          
    148.     }
    149.         if (GUI.Button(Rect(145,0,22,22), btnRotDown)) {
    150.  
    151.          
    152.     }
    153.         if (GUI.Button(Rect(200,0,22,22), btnZoomIn)) {
    154.  
    155.          
    156.     }
    157.         if (GUI.Button(Rect(200,30,22,22), btnZoomOut)) {
    158.  
    159.          
    160.     }
    161.     //END Control BTNs//
    162.     GUI.EndGroup();
    163.     //Animation BTNs//
    164.  
    165.    
    166.     if (GUI.Button(Rect(290,15,22,22), "A")) {
    167.         newPosition = positionA;
    168.         newRotation = rotationA;
    169.         dragging = false;
    170.     }
    171.    
    172.     if (GUI.Button(Rect(320,15,22,22), "B")) {
    173.         newPosition = positionB;
    174.         newRotation = rotationB;
    175.         dragging = false;
    176.     }
    177.    
    178.     if (GUI.Button(Rect(3,15,22,22), "C")) {
    179.         newPosition = positionC;
    180.         newRotation = rotationC;
    181.         dragging = false;
    182.     }
    183.    
    184.     //End Animation BTNs//
    185.    
    186.  
    187.  
    188.     //End GUI Buttons//
    189.    
    190.    }
     
  22. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Code (JavaScript):
    1.  
    2. function Update ()
    3. {
    4.     if (!dragging)
    5.         MoveRotate ();
    6.      
    7.    //Spin object
    8.    if (Input.GetMouseButton(0) && dragging)
    9.    {
    10.          speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
    11.          avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
    12.    }
    13.    else
    14.    {
    15.          if (dragging)
    16.          {
    17.              speed = avgSpeed;
    18.              //dragging = false;       <<<<<<<< There was one more
    19.          }
    20.          var i = [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Time']Time[/URL].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=deltaTime']deltaTime[/URL] * lerpSpeed;
    21.           [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=speed']speed[/URL] = [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Vector3']Vector3[/URL].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Lerp']Lerp[/URL]( [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=speed']speed[/URL], [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Vector3']Vector3[/URL].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=zero']zero[/URL], i);
    22.      }
    23.      transform.[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Rotate']Rotate[/URL]( [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Camera']Camera[/URL].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=main']main[/URL].transform.up * [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=speed']speed[/URL].x * rotationSpeed, [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Space']Space[/URL].World );
    24.      transform.[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Rotate']Rotate[/URL]( [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Camera']Camera[/URL].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=main']main[/URL].transform.[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=right']right[/URL] * [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=speed']speed[/URL].y * rotationSpeed, [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Space']Space[/URL].World );
    25.      //End Spin Object
    26. }
    27.  
     
  23. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Wow, Thankyou so much Fraconte! That is awesome, now i understand what you mean by getting it to stop.
    Thanks again, Jamuk.:)