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

What my boss requires me to do is exactly same like this:
799eeb301b8c425fb23addfbe80add76

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…

using UnityEngine;
using System.Collections;

public class OrbitCamera : MonoBehaviour
{
    ColorPanel _colorPanel;

    public GameObject target;
    public Transform[] cam;

    [SerializeField] float xSpeed = 15.0f;
    [SerializeField] float ySpeed = 15.0f;
    [SerializeField] float scrollSpeed = 5.0f;

    float velocityX = 0.0f;
    float velocityY = 0.0f;
    float velocityScroll = 0.0f;

    float rotationXAxis = 0.0f;
    float rotationYAxis = 0.0f;

    [SerializeField]    float yMinLimit = -20f;
    [SerializeField]    float yMaxLimit = 80f;

    public float distance = 1.5f;
 
    void Start()
    {
        _colorPanel = GameObject.FindGameObjectWithTag("ColorPanel").GetComponent<ColorPanel>();
        Vector3 camAngle = transform.eulerAngles;
        rotationXAxis = camAngle.x;
        rotationYAxis = camAngle.y;
    }
 
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            if(Physics.Raycast(ray, out hit))
            {
                hit.transform.GetComponent<MeshRenderer>().material = _colorPanel.currentMat;
            }
        }
    }

    void LateUpdate()
    {
        Orbit();
    }

    void Orbit()
    {
        if(target != null)
        {
            if(Input.GetMouseButton(0))
            {
                velocityX += xSpeed * Input.GetAxis("Mouse X") *  distance * Time.deltaTime;
                velocityY += ySpeed * Input.GetAxis("Mouse Y") * Time.deltaTime;
            }
            velocityScroll += scrollSpeed * Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime;

            rotationYAxis += velocityX;
            rotationXAxis -= velocityY;

            rotationXAxis = Mathf.Clamp(rotationXAxis, yMinLimit, yMaxLimit);
         
            Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
            Quaternion rotation = toRotation;
         
            distance = Vector3.Distance(transform.position, target.transform.position);
            distance = Mathf.Clamp(distance - velocityScroll, 2, 5);
            Vector3 position = rotation * new Vector3(0, 0, -distance) + target.transform.position;
         
            transform.position = position;
            transform.rotation = rotation;
         
            velocityX = Mathf.Lerp(velocityX, 0, 2 * Time.deltaTime);
            velocityY = Mathf.Lerp(velocityY, 0, 2 * Time.deltaTime);
            velocityScroll = Mathf.Lerp(velocityScroll, 0, Time.deltaTime);
        }
        else
        {
            Debug.LogWarning("Orbit Camera - No Target Set");
        }
    }

    void ChgView(int index)
    {
        rotationXAxis = cam[index - 1].transform.eulerAngles.x;
        rotationYAxis = cam[index - 1].transform.eulerAngles.y;
    }
}

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 :wink:

For example:

Move from current position to the target in 2 secs.

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

Now try doing it with rotation as well.

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.

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!

1 Like

I’d check out GoKIT by Prime31 too. Pretty solid and light weight tween lib.

1 Like

currently it won’t work.
how can i plug this into my scripts?
what i guess is i 've already set the

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?

no work, no life, no choice…

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

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;

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.

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

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.
2191539--145410--螢幕快照 2015-07-07 上午1.46.55.png