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 - Fatal编程技术网

C# 检查播放机和相机之间的重叠

C# 检查播放机和相机之间的重叠,c#,unity3d,C#,Unity3d,告诉我如何隐藏对象的顶层 我现在就是这样。 图层顺序: 内部 玩家 建筑 我不知道如何隐藏建筑层 据我所知,我需要Raycast 2d? 但我不知道如何使用它。我需要创建一个内部层,例如在Z=1时,玩家在Z=1.1时,建筑在Z=1.2时,或者什么 我的代码: void FixedUpdate() { rigidbody.MovePosition(rigidbody.position + movement * movementSpeed * Time.fixedDeltaTime);

告诉我如何隐藏对象的顶层

我现在就是这样。 图层顺序: 内部 玩家 建筑 我不知道如何隐藏建筑层

据我所知,我需要Raycast 2d? 但我不知道如何使用它。我需要创建一个内部层,例如在Z=1时,玩家在Z=1.1时,建筑在Z=1.2时,或者什么

我的代码:

void FixedUpdate() {
    rigidbody.MovePosition(rigidbody.position + movement * movementSpeed * Time.fixedDeltaTime);
    int layer_mask = LayerMask.GetMask("House");
    RaycastHit hit;
    // Does the ray intersect any objects excluding the player layer
    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 5, layer_mask))
    {
        Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
        Debug.Log("Did Hit ");
    }
    else
    {
        Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
        Debug.Log("Did not Hit " + LayerMask.GetMask("House"));
    }
}
输出:“未击中”,由于某种原因,光束是从角色发射的,而不是从相机发射的

如果我将代码设置为:

Transform maincam = GameObject.Find("Camera").GetComponent<Transform>();
transformmaincam=GameObject.Find(“摄影机”).GetComponent();
利用这个位置,光束从摄像机中射出,但看不到建筑物。

使用System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Controller : MonoBehaviour
{
    public float movementSpeed = 1f;   //Movement Speed of the Player
    public Vector2 movement;           //Movement Axis
    public Rigidbody2D rigidbody;      //Player Rigidbody Component
    public Animator animator;
    public Camera cam;
    public float floatHeight;     // Desired floating height.
    public float liftForce;       // Force to apply when lifting the rigidbody.
    public float damping;         // Force reduction proportional to speed (reduces bouncing).

    Rigidbody2D rb2D;
    
    private void Start()
    {
        rigidbody = this.GetComponent<Rigidbody2D>();
        animator = this.GetComponent<Animator>();
        rb2D = GetComponent<Rigidbody2D>();
    }

    private void Update()
    { 
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");
        animator.SetFloat("Horizontal", movement.x);
        animator.SetFloat("Vertical", movement.y);
        animator.SetFloat("Speed", movement.sqrMagnitude);
    }

    
    void FixedUpdate() {
        
        rigidbody.MovePosition(rigidbody.position + movement * movementSpeed * Time.fixedDeltaTime);
        
        // Cast a ray straight down.
        RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);

        // If it hits something...
        if (hit.collider != null)
        {
            if(hit.collider.gameObject.layer == 11){
                hit.collider.gameObject.GetComponent<Renderer>().enabled = false;
            }else{
                hit.collider.gameObject.GetComponent<Renderer>().enabled = true;
            }
        }
    }
}
使用System.Collections.Generic; 使用UnityEngine; 公共类控制器:单行为 { 公共浮动移动速度=1f;//播放器的移动速度 公共向量2移动;//移动轴 公共刚体2d刚体;//播放器刚体组件 公共动画师; 公共摄像机; 公共浮动高度;//所需的浮动高度。 公共浮动提升力;//提升刚体时应用的力。 公共浮动阻尼;//与速度成比例的力减小(减少反弹)。 刚体2d rb2D; 私有void Start() { 刚体=this.GetComponent(); animator=this.GetComponent(); rb2D=GetComponent(); } 私有void更新() { movement.x=Input.GetAxisRaw(“水平”); movement.y=Input.GetAxisRaw(“垂直”); animator.SetFloat(“水平”,movement.x); animator.SetFloat(“垂直”,movement.y); 动画师.SetFloat(“速度”,运动量.sqrMagnitude); } void FixedUpdate(){ 刚体.移动位置(刚体.位置+移动*移动速度*时间.固定时间); //直接向下投射光线。 RaycastHit2D hit=Physics2D.Raycast(transform.position,-Vector2.up); //如果它击中了什么东西。。。 if(hit.collider!=null) { if(hit.collider.gameObject.layer==11){ hit.collider.gameObject.GetComponent().enabled=false; }否则{ hit.collider.gameObject.GetComponent().enabled=true; } } } }
还没有解决如何打开show back的问题