简体   繁体   中英

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. 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. 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. Search up "Raycast tutorial" and you will find many results.

Within pickup you define int x and int y inside of an if block.

The second issue is that within (or after) the pickup method you have one closing } to much.

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 . 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

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 . 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;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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