简体   繁体   English

如何为 Unity 3D 修复此脚本

[英]How do I fix this script for Unity 3D

The script is supposed to allow my first person controller/player go up to an object, press the E key and then pickup and carry the object around.该脚本应该允许我的第一人称控制器/播放器 go 到 object,按 E 键然后拿起并随身携带 object。 There are errors in the script and I don't understand how to program yet.脚本中有错误,我还不明白如何编程。 I've also attached the screenshot of the errors in the code for reference.我还附上了代码中错误的屏幕截图以供参考。

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

public class PickupObject : MonoBehaviour
{
    GameObject mainCamera;
    bool carrying;
    GameObject carriedObject;
    public float distance;
    public float smooth;
    // Start is called before the first frame update
    void Start()
    {
        mainCamera = GameObject.FindWithTag("MainCamera");
    }

    // Update is called once per frame
    void Update()
    {
        if (carrying)
        {
            carry(carriedObject);
            checkDrop();
        }
        else
        {
            pickup();
        }
    }

    void carry(GameObject o)
    {
        o.GetComponent<Rigidbody>().isKinematic = true;
        o.transform.position = Vector3.Lerp (mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
    }

    void pickup() 
    {
        if (Input.GetKeyDown KeyCode.E;))
        {
            int x = Screen.width / 2;
            int y = Screen.height / 2;
        }

        Ray ray = mainCamera.GetComponent<Camera>().ScreentPointToRay(new Vector3(x, y));
        RaycastHit hit;
        if(Physics.Raycast(ray, out hit))
        {
            Pickupable p = hit.collider.GetComponent<Pickupable>();
            if(p != null)
            {
                carrying = true;
                carriedObject = p.gameObject;
                p.gameObject.rigidbody.isKinematic = true;
            }
        }
    }
}

void checkDrop()
{
    if(Input.GetKeyDown(KeyCode.E))
    {
        dropObject();
    }
    void dropObject()
    {
        carrying = false;
        carriedObject = null;
        carriedObject.gameObject.rigidbody.isKinematic = false;
    }
}

} }

在此处输入图像描述

Maybe you should try using Raycasts to pickup items.也许您应该尝试使用 Raycast 来拾取物品。 Create a "Pick Up" tag, add that tag to all pickup-able items, shoot a Raycast from the camera in the direction of the camera if the player presses 'E', check if the hit has the tag, then pick it up.创建一个“Pick Up”标签,将该标签添加到所有可拾取物品,如果玩家按下“E”,则从相机向相机方向发射 Raycast,检查命中是否有标签,然后将其拾取. Search up "Raycast tutorial" and you will find many results.搜索“Raycast 教程”,你会发现很多结果。

Within pickup you define int x and int y inside of an if block.pickup中,您在if块中定义int xint y

The second issue is that within (or after) the pickup method you have one closing } to much.第二个问题是在pickup方法之内(或之后)你有一个关闭}到很多。

if (Input.GetKeyDown KeyCode.E;))
{
    int x = Screen.width / 2;
    int y = Screen.height / 2;
} // <-- SEEMS THAT THIS HERE IS YOUR PROBLEM !

So you basically end your class before the method checkDrop .所以你基本上在方法 checkDrop 之前结束你的checkDrop The rest are just follow up errors: The x and y will be known only within this code block and when you later try to use them in rest 只是后续错误: xy仅在此代码块中是已知的,当您稍后尝试在

Ray ray = mainCamera.GetComponent<Camera>().ScreentPointToRay(new Vector3(x, y));

they do not exist.它们不存在。

Also as said you and the class so the method checkDrop is not known in Update .也正如您和 class 所说的那样,方法checkDropUpdate中是未知的。 And then you get some additional errors anyway since it is not allowed to define a method outside of a type.然后你会得到一些额外的错误,因为不允许在类型之外定义方法。


Note that I formatted your code so it should be quite clear now.请注意,我格式化了您的代码,所以现在应该很清楚了。 You probably rather wanted it do to be你可能更希望它做

void pickup() 
{
    if (Input.GetKeyDown KeyCode.E;))
    {
        int x = Screen.width / 2;
        int y = Screen.height / 2;
    
        Ray ray = mainCamera.GetComponent<Camera>().ScreentPointToRay(new Vector3(x, y));
        RaycastHit hit;
        if(Physics.Raycast(ray, out hit))
        {
            Pickupable p = hit.collider.GetComponent<Pickupable>();
            if(p != null)
            {
                carrying = true;
                carriedObject = p.gameObject;
                p.gameObject.rigidbody.isKinematic = true;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM