【发布时间】:2020-08-01 17:53:48
【问题描述】:
该脚本应该允许我的第一人称控制器/播放器上到一个对象,按 E 键然后拾取并携带该对象。脚本中有错误,我还不明白如何编程。我还附上了代码中的错误截图以供参考。
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;
}
}
}
【问题讨论】:
-
即使你不完全理解,解决第一个问题,检查它,看看它现在说什么......等等。
标签: c# unity3d game-development