首页 文章

移动相机并将其旋转到目标游戏对象

提问于
浏览
-1

我在场景中有六个不同的空游戏对象,有6个不同的按钮 . 当我点击按钮1时,相机将朝向游戏对象1移动并根据目标游戏对象自行旋转但是当相机移向第3个游戏对象时,我的相机将以不同的随机方式开始移动以及随机旋转的不同这是由float t = 0.0f;在代码中,当我调用public void Wallview()然后我的相机移动到对象3但它开始随机旋转随机旋转请帮助我谢谢

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

公共类camMOVEtwo:MonoBehaviour {

public Transform  handleview;
public Transform pressureview;
public Transform wallview;
public Transform sechandleview;
public Transform pressuretwoview;
public Transform switchview;

public GameObject handlebtn;
public GameObject pressurebtn;
public GameObject wallbtn;
public GameObject handletwobtn;
public GameObject pressuretwobtn;
public GameObject switchbtn;




public float transitionSPEED;
Transform currentVIEW;
public bool flag = false;
public bool isStarted = false;
Vector3 currentangel;
public List<GameObject> modelparts;

private void Start(){
    handlebtn.SetActive (true);
    pressurebtn.SetActive (false);
    wallbtn.SetActive (false);
    handletwobtn.SetActive (false);
    pressuretwobtn.SetActive (false);
    switchbtn.SetActive (false);





    foreach (GameObject obj in modelparts) {

        obj.GetComponent<BoxCollider> ().enabled = false;
    }
}

private void Update(){

    if (flag && !isStarted) {

        StartCoroutine (newnew ());
        isStarted = true;

    }


}

IEnumerator newnew(){
    float t = 0.0f;
    while (t < 2.0f) {
        t += Time.deltaTime;
        transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);


        currentangel = new Vector3 (Mathf.LerpAngle (transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSPEED),
            Mathf.LerpAngle (transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSPEED),
            Mathf.LerpAngle (transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSPEED));

        transform.eulerAngles = currentangel;
        Debug.Log ("coroutine is running");

        yield return null;

    }



}

public void Handleview(){

    currentVIEW = handleview;
    handlebtn.SetActive (false);

    flag = true;
    isStarted = false;


}

public void Pressureview(){

    currentVIEW = pressureview;
    pressurebtn.SetActive (false);
    flag = true;
    isStarted = false;

}

public void Wallview(){

    currentVIEW = wallview;
    wallbtn.SetActive (false);
    flag = true;
    isStarted = false;



}

public void Secondhandleview(){
    currentVIEW = sechandleview;
    handletwobtn.SetActive (false);
    flag = true;
    isStarted = false;



}

public void Pressuretwoview(){
    currentVIEW = pressuretwoview;
    pressuretwobtn.SetActive (false);
    flag = true;
    isStarted = false;



}

public void Switchview(){
    currentVIEW = switchview;
    switchbtn.SetActive (false);
    flag = true;
    isStarted = false;



}

}

2 回答

  • 0

    大部分答案都是重复 . 仍然完全重写它为其他读者的背景 .

    您可以获取 points 数组,如下面的脚本所示 . 另外为了保持相机的位置,我采用变量 currentPointIndex 只是为了保持当前点的索引 .

    为了正确旋转,请在协程内使用 Quaternion.RotateTowards(); ,如下面的代码所示

    CameraMotion.cs

    public Transform[] points;
    public int currentPointIndex=0;
    public Transform lookAtTarget;
    
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartCoroutine(CameraTransition(points[currentPointIndex],1.0f));
            currentPointIndex++;
        }
    }
    
    IEnumerator CameraTransition(Transform nextPoint,float time)
    {
        float i = 0;
        float rate = 1 / time;
    
        Vector3 fromPos = transform.position;
    
        while (i<1)
        {
            i += Time.deltaTime * rate;
            transform.position = Vector3.Lerp(fromPos,nextPoint.position,i);
    
    
            Quaternion targetRotation = Quaternion.LookRotation(nextPoint.position-transform.position);
            transform.rotation = Quaternion.RotateTowards(transform.rotation,targetRotation,i);
    
            yield return 0;
        }
    }}
    

    基本上,我们循环遍历数组点并相应地进行转换 .

    或者,如果您的点不是线性形式(在数组中),则可以相应地修改逻辑 . 但是你可以使用相同的Co-routine .

  • 0

    我建议让事情变得更好,首先不要让事情硬编码让它接受更多的对象 . 第二个坚持用更简单的解决方案 .

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class LookAtObject : MonoBehaviour {
    
    public float Speed = 0.001f;
    private Transform _go;
    
    // Update is called once per frame
    void Update ()
    {
        if (_go == null) return;
    
        Vector3 direction = _go.transform.position - transform.position;
        Quaternion toRotation = Quaternion.FromToRotation(transform.forward, direction);
        transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, Speed * Time.time);
    }
    
    public void AdjustOrientation(Transform go)
    {
        _go = go;
    }
    }
    
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class OnClick : MonoBehaviour
    {
    private LookAtObject _lookAt;
    
    
    public void Start()
    {
        _lookAt = FindObjectOfType<LookAtObject>();
    }
    
    public void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
    
            if (Physics.Raycast(ray, out hit, 100))
            {
                //optionally if(hit.collider.tag != "MyTag") return;
                _lookAt.AdjustOrientation(hit.transform);
            }
        }
    }
    }
    

    将这些类放在您的相机上,会发生什么事情是通过AdjustOrientation在您想要查看的对象中获取的第一类 . 更新将在填充后查看该对象 .

    第二类允许您单击包含刚体的场景中的任何对象,并且是Kinematic . 单击对象后,它将设置AdjustOrientations使相机查看该对象,您可以轻松地将标记添加到gameObject并说明hit.collider.tag ==“LookAt”然后允许它查看 .

相关问题