Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Urgent! Help me plz, or i am fired....911 over....

Discussion in 'Scripting' started by manlaikin1994, Jul 6, 2015.

  1. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    What my boss requires me to do is exactly same like this:


    螢幕快照 2015-07-06 下午11.03.24.png 螢幕快照 2015-07-06 下午11.03.42.png

    I have done the smooth swipe successfully.
    However, i have no idea to do the "Change view" when you click the letter button.
    I tried the :
    void ChgView(int index)
    {
    rotationXAxis = cam[index - 1].transform.eulerAngles.x;
    rotationYAxis = cam[index - 1].transform.eulerAngles.y;
    }

    but the result of rotation is not same as the reference....
    And i want to do it in lerp way.
    But i have no idea how to do it now. help me plz..........

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class OrbitCamera : MonoBehaviour
    5. {
    6.     ColorPanel _colorPanel;
    7.  
    8.     public GameObject target;
    9.     public Transform[] cam;
    10.  
    11.     [SerializeField] float xSpeed = 15.0f;
    12.     [SerializeField] float ySpeed = 15.0f;
    13.     [SerializeField] float scrollSpeed = 5.0f;
    14.  
    15.     float velocityX = 0.0f;
    16.     float velocityY = 0.0f;
    17.     float velocityScroll = 0.0f;
    18.  
    19.     float rotationXAxis = 0.0f;
    20.     float rotationYAxis = 0.0f;
    21.  
    22.     [SerializeField]    float yMinLimit = -20f;
    23.     [SerializeField]    float yMaxLimit = 80f;
    24.  
    25.     public float distance = 1.5f;
    26.  
    27.     void Start()
    28.     {
    29.         _colorPanel = GameObject.FindGameObjectWithTag("ColorPanel").GetComponent<ColorPanel>();
    30.         Vector3 camAngle = transform.eulerAngles;
    31.         rotationXAxis = camAngle.x;
    32.         rotationYAxis = camAngle.y;
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         if(Input.GetMouseButtonDown(0))
    38.         {
    39.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    40.             RaycastHit hit;
    41.             if(Physics.Raycast(ray, out hit))
    42.             {
    43.                 hit.transform.GetComponent<MeshRenderer>().material = _colorPanel.currentMat;
    44.             }
    45.         }
    46.     }
    47.  
    48.     void LateUpdate()
    49.     {
    50.         Orbit();
    51.     }
    52.  
    53.     void Orbit()
    54.     {
    55.         if(target != null)
    56.         {
    57.             if(Input.GetMouseButton(0))
    58.             {
    59.                 velocityX += xSpeed * Input.GetAxis("Mouse X") *  distance * Time.deltaTime;
    60.                 velocityY += ySpeed * Input.GetAxis("Mouse Y") * Time.deltaTime;
    61.             }
    62.             velocityScroll += scrollSpeed * Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime;
    63.  
    64.             rotationYAxis += velocityX;
    65.             rotationXAxis -= velocityY;
    66.  
    67.             rotationXAxis = Mathf.Clamp(rotationXAxis, yMinLimit, yMaxLimit);
    68.          
    69.             Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
    70.             Quaternion rotation = toRotation;
    71.          
    72.             distance = Vector3.Distance(transform.position, target.transform.position);
    73.             distance = Mathf.Clamp(distance - velocityScroll, 2, 5);
    74.             Vector3 position = rotation * new Vector3(0, 0, -distance) + target.transform.position;
    75.          
    76.             transform.position = position;
    77.             transform.rotation = rotation;
    78.          
    79.             velocityX = Mathf.Lerp(velocityX, 0, 2 * Time.deltaTime);
    80.             velocityY = Mathf.Lerp(velocityY, 0, 2 * Time.deltaTime);
    81.             velocityScroll = Mathf.Lerp(velocityScroll, 0, Time.deltaTime);
    82.         }
    83.         else
    84.         {
    85.             Debug.LogWarning("Orbit Camera - No Target Set");
    86.         }
    87.     }
    88.  
    89.     void ChgView(int index)
    90.     {
    91.         rotationXAxis = cam[index - 1].transform.eulerAngles.x;
    92.         rotationYAxis = cam[index - 1].transform.eulerAngles.y;
    93.     }
    94. }
    95.  
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,228
    Wow no pressure.

    I suggest using iTween(or something similar), it will give you more to play with than a simple lerp and its easier to work with ;)

    For example:

    Move from current position to the target in 2 secs.
    Code (csharp):
    1. iTween.MoveTo(gameObject,iTween.Hash("position",cam[index - 1].position,"time",2));
    Now try doing it with rotation as well.
     
  3. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Try to stick to Quaternions as much as possible. The angles you put into a Quaternion are not always the same you get out after, even though the rotation is the same.

    Also, I don't think you should use click-baity thread titles.
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Oh boy, maybe signed up for something without the proper preparation? Anyway yeah try a tween framework - lean tween, dotween, or itween (others out there) and wrap your head around it, testing it in another project, then come back and get it going on this project.

    Good luck!
     
    karl_jones likes this.
  5. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    I'd check out GoKIT by Prime31 too. Pretty solid and light weight tween lib.
     
    karl_jones likes this.
  6. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    currently it won't work.
    how can i plug this into my scripts?
    what i guess is i 've already set the
    1. transform.position = position;
    2. transform.rotation = rotation;
    in void Update function. And the "public void ChgView(int index)" work once only if i click.
    therefore, there are no effect. how to fix this?
     
  7. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    no work, no life, no choice.......
     
  8. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    just search for the smooth follow camera script and place the point and the target, it won't look great since you are not using splines but it will get the job done
     
  9. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    Thx everyone. I open a new project and do it again with the same scrips and it work.
    i have no idea why.
    i am still using this in public void chg view function,
    but how can it works in lerp if i don't want to put it into update function?
    rotationXAxis = cam[index - 1].transform.eulerAngles.x;
    rotationYAxis = cam[index - 1].transform.eulerAngles.y;
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,228
    Did you import the iTween package from the asset store?

    Just call the iTween commands from your ChgView function, they only need to be called once.
     
  11. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    am i miss it?
    i don't find sth like mathf.lerp function in itween douc

    PS: iTween.MoveTo(gameObject,iTween.Hash("position",cam[index - 1].position,"time",2));
    iTween.RotateTo(gameObject,iTween.Hash("rotation",cam[index - 1].rotation,"time",2));

    doesn't work in my scritps
     
  12. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    when i solve a problem, another problem always come....
    i want to know how to assign this through scripts in the beginning, since when i instantiate a prefab that contain a 3d ugui, it loss the reference.
    螢幕快照 2015-07-07 上午1.46.55.png