Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一:相机对撞机_C#_Unity3d_Camera_Collision - Fatal编程技术网

C# 统一:相机对撞机

C# 统一:相机对撞机,c#,unity3d,camera,collision,C#,Unity3d,Camera,Collision,我正在为一个学校项目创建一个第三人游戏,但我在碰撞方面遇到了一些问题:我小时候有一个带相机的玩家,他有一个球体碰撞器。当相机与任何风景对象(如房屋)碰撞时,它应该缩小。一旦离开碰撞位置,它应返回到其原来的位置,其局部y应为4.5。现在,当我站着不动时,我遇到了以下问题:相机不断地离开并进入物体的碰撞器,这导致它不断地放大和缩小。这导致了一个看起来非常浮躁的相机运动。有没有办法解决这个问题 我使用了以下代码: using System.Collections; using System.Colle

我正在为一个学校项目创建一个第三人游戏,但我在碰撞方面遇到了一些问题:我小时候有一个带相机的玩家,他有一个球体碰撞器。当相机与任何风景对象(如房屋)碰撞时,它应该缩小。一旦离开碰撞位置,它应返回到其原来的位置,其局部y应为4.5。现在,当我站着不动时,我遇到了以下问题:相机不断地离开并进入物体的碰撞器,这导致它不断地放大和缩小。这导致了一个看起来非常浮躁的相机运动。有没有办法解决这个问题

我使用了以下代码:

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



public class CamMovement : MonoBehaviour
 {
     public GameObject Parent;

     //Checks if the camera collides with something
     void OnTriggerStay(Collider other)
     {
         //When colliding, the camera moves up and back from the player object          
         transform.position += new Vector3(0, 0.2f, -0.2f);    
     }


     void Update()
     {
         //makes sure the camera always looks at the player object   
         transform.LookAt(Parent.transform);

         //Moves the camera back to the normal (local) position
         if (transform.localPosition.y > 4.5f)
         {
             transform.position += new Vector3(0, Time.deltaTime * -4f, Time.deltaTime * 4f);
         }
     }
 }

摄像机与物体碰撞时的画面:

我不确定我是否正确理解了你想要实现的目标,但我会给它一个镜头:

我认为您应该查看OnTiggerEnter和OnTiggerExit事件,这样您就可以告诉相机在输入触发器时移开,在退出触发器时移开

public class CamMovement : MonoBehaviour
{
    //using "parent" as variable name is not recommended since Transform class already contains a parent variable
    [SerializeField]
    private GameObject parentToLookAt;
    [SerializeField]
    private Vector3 localPositionOffset;
    [Range(0.0f, 10.0f)]
    [SerializeField]
    private float transitionSpeed;

    private Vector3 localPositionOnStart;
    private bool applyOffset;

    void Start()
    {
        localPositionOnStart = transform.localPosition;
        applyOffset = false;
    }

    void Update()
    {
        //Makes sure the camera always looks at the player object
        //You can also use: transform.LookAt(transform.parent); 
        transform.LookAt(parentToLookAt.transform);

        //Moves the camera to the right local position (note that using Mathf.Lerp is not optimal performance-wise but if you want more info on this
        //I recommend looking for further informations at https://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/ )
        if (applyOffset)
        {
            transform.localPosition = Mathf.Lerp(transform.localPosition, localPositionOnStart + localPositionOffset, transitionSpeed * Time.deltaTime);
        }
        else
        {
            transform.localPosition = Mathf.Lerp(transform.localPosition, localPositionOnStart, transitionSpeed * Time.deltaTime);
        }
    }

    //Checks if the camera collides with something
    void OnTriggerEnter(Collider other)
    {
        applyOffset = true;
    }

    //Checks if the camera stops colliding with something
    void OnTriggerExit(Collider other)
    {
        applyOffset = false;
    }

    //You can also use this:
    //void OnTriggerStay(Collider other)
    //{
    //    applyOffset = true;
    //}
    // and set applyOffset to false at the end of the Update() method (OnTrigger events are called before Update each frame)
}
还有两件事:

当用C语言编程时,通常的规则是不使用大写字母来开始变量名。这是保留给类名的:您可以检查全局准则 还可以使用[SerializeField]属性序列化私有变量,从而使其显示在检查器中。我还添加了[Range]属性,这在与不喜欢原始数字的设计师合作时非常有用;
您需要检查哪个碰撞器与摄影机碰撞器发生碰撞,您可以使用OnColliderIntercollider中的类似结构来实现这一点,其他:


你可以把一个标签放在玩家对象上,然后得到它的相机对撞机,然后做如果!其他= = PraveCurrdule{ZoMoOut};如果这解决了你的问题,请让我知道,我会把它作为一个正确的答案。如果有任何答案解决了你的问题,请考虑接受它作为正确的。接受答案有助于未来的访问者访问此页面我可能错了,但我认为你误解了这个问题。@JamesHughes是的,我不确定:我不知道问题是否与对撞机碰撞的任何东西有关,这些东西需要像你在回答中所做的那样进行快速测试,即使在我的回答中也是有效的:在这种情况下,应始终进行测试,以避免任何不必要的碰撞。或者可能是因为当相机离开玩家时,相机碰撞器不再碰撞,所以它开始向后移动,然后不停地来回移动,无法从问题中真正了解层次结构是如何组织的。。。。
Collider playerCollider = GameObject.Fine("Player").GetComponent<Collider>();
if (!other == playerCollider)
{
//Do your zooming out.
}