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

MouseOrbit Return behind the character

Discussion in 'Scripting' started by TheDeathKnight, Aug 12, 2015.

  1. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Hey guys,
    I'm trying to make a MouseOrbit, but I get hard time with something.
    When I press on the left mouse button, I can rotate the camera and when I release it, the camera come back smoothly behind the character. However, if I click a second time on the mouse left button, the camera move toward to the last camera position at the time that I released the button. Basically, the camera doesn't stay behind the character when I press on the mouse.
    Honestly, I'm working hard to find where's the issue, I tried some things, but I can figure out how to fix it.



    Code (CSharp):
    1. void Update () {
    2.  
    3.         OriginRot = Target.rotation;
    4.         OriginRot *= Quaternion.Euler (25f, 0f, 0f);
    5.  
    6.         Origin = Target.TransformPoint (new Vector3 (0f, 8f, -15f));
    7.  
    8.  
    9.        
    10.         if (Input.GetMouseButton (0)) {
    11.            
    12.  
    13.             x += Input.GetAxis("Mouse X") * 20;
    14.             y += Input.GetAxis("Mouse Y") * 20;
    15.  
    16.             rotation = Quaternion.Euler(y , x , 0);
    17.  
    18.             negDistance = new Vector3(0f, 0f, -15f);
    19.             position = rotation * negDistance + Target.position;
    20.            
    21.             transform.rotation = rotation;
    22.             transform.position = position;
    23.  
    24.  
    25.         }
    26.         else
    27.         {
    28.             transform.position =  Vector3.Lerp (transform.position, Origin, Time.deltaTime * 5);
    29.            
    30.             transform.rotation = Quaternion.Slerp (transform.rotation, OriginRot, Time.deltaTime * 5);
    31.  
    32.  
    33.         }
    34.        
    35.     }
    36. }
    Basically, I think I need to always keep the camera position in a variable, but I don't know which variable I need to give this position.
     
  2. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Any clue ?
     
  3. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
  4. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
  5. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    Comment out the 'else' section of the script and see what happens.
     
  6. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Basically, the "else" section is to smoothly rotate the camera to the origin position and it works, but when I press on the mouse the camera move toward to the last position and doesn't stay to the origin.

    After few hours/days, I think the issue is the x,y save the last position, but I don't know how to fix it.
     
  7. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    The main issue here is to rotate the camera behind the character (the origin position) after rotated the camera around this character.
    I'm trying to find a way to do that with this code. Basically the "else" is only the half of the solution that I found



    Code (CSharp):
    1. if (Input.GetMouseButton (0)) {
    2.  
    3.             x += Input.GetAxis("Mouse X") * 20;
    4.             y += Input.GetAxis("Mouse Y") * 20;
    5.  
    6.             rotation = Quaternion.Euler(y , x , 0);
    7.  
    8.             negDistance = new Vector3(0f, 0f, -15f);
    9.             position = rotation * negDistance + Target.position;
    10.          
    11.             transform.rotation = rotation;
    12.             transform.position = position;
    13.  
    14.  
    15.         }
     
  8. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    I've worked with MouseOrbit. I came across issues when Saving/Loading from XML, I would set the position and rotation of the camera but it would still be incorrect. I realized it was the x and y variables from that script. Simply set the x and y variables to be the desired position. (The x and y are what determines the rotation when the script is behaving normally) So if you'll need to set x and y at the end of your Lerp. (This may prove complicated if you want it to be dynamic--you will have to calculate x and y based off of the position of camera versus the origin. Reverse engineering is required)
     
  9. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Hi, I tried that, but whatever the value I set for x and y, the camera still going on the left.

    X and Y should be behind the character ( Target.transformpoint), but I don't know how to do that.

    I just tried something else, but I really can't fix it.
     
    Last edited: Aug 14, 2015
  10. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Basically, I want X and Y have the same value that transform.position before pressing button.
     
  11. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Can I use Target.transformpoint like Target.transform.position.x, can I give the x position based on the character position.
     
  12. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35