Javascript 拾取和移动周围的对象
我试着在c#中找到一个例子,把一个物体放到玩家的手的位置,然后四处移动。我在unity论坛上找到了一个例子,但是它在C#中如何实现呢 这是我找到的代码,但我需要在单击按钮时抓取它,播放器需要在对象前面Javascript 拾取和移动周围的对象,javascript,c#,unity3d,Javascript,C#,Unity3d,我试着在c#中找到一个例子,把一个物体放到玩家的手的位置,然后四处移动。我在unity论坛上找到了一个例子,但是它在C#中如何实现呢 这是我找到的代码,但我需要在单击按钮时抓取它,播放器需要在对象前面 #pragma strict var TheSystem : Transform; var Distance : float; var MaxDistance : float = 10; function Update() { var hit : RaycastHit;
#pragma strict
var TheSystem : Transform;
var Distance : float;
var MaxDistance : float = 10;
function Update() {
var hit : RaycastHit;
if (Physics.Raycast (TheSystem.transform.position, TheSystem.transform.TransformDirection(Vector3.forward), hit))
{
if(hit.transform.gameObject.tag == "sword2"){
Distance = hit.distance;
if (Distance < MaxDistance){
if (Input.GetKeyDown(KeyCode.E)) {
// show
renderer.enabled = true;
Destroy (GameObject.FindWithTag("sword2"));
}
if (Input.GetKeyDown(KeyCode.Backspace)) {
// hide
renderer.enabled = false;
}
}
}
}
}
为了实现你心中的目标,有必要将许多事情结合起来
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moveObject : MonoBehaviour {
public GameObject handEmptyGameObject;
GameObject item = null;
bool objectOnRange = false;
// Use this for initialization
void Start () {
//This is useful if you have just one item to collect in your scene
//if you have more than one, better remove it
item = GameObject.FindGameObjectsWithTag("item");
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.G) && objectOnRange)
{
print("Grabbing an object");
item.transform.position = hand.transform.position;
item.transform.SetParent(hand.transform,true);
}
}
//You need to tag the GameObjec tto grab as "item" and set a
//collider and rigid bodies in the GameObjects
//This is to estimate if the player is close enough to the Object
void OnTriggerEnter(Collider other)
{
if(other.tag == "item")
{
objectOnRange = true;
item = other.gameObject;
}
}
void OnTriggerExit(Collider other)
{
if(other.tag == "item")
{
objectOnRange = false;
item = other.gameObject;
}
}
}
你在哪里尝试“翻译”这个?你遇到了什么问题?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moveObject : MonoBehaviour {
public GameObject handEmptyGameObject;
GameObject item = null;
bool objectOnRange = false;
// Use this for initialization
void Start () {
//This is useful if you have just one item to collect in your scene
//if you have more than one, better remove it
item = GameObject.FindGameObjectsWithTag("item");
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.G) && objectOnRange)
{
print("Grabbing an object");
item.transform.position = hand.transform.position;
item.transform.SetParent(hand.transform,true);
}
}
//You need to tag the GameObjec tto grab as "item" and set a
//collider and rigid bodies in the GameObjects
//This is to estimate if the player is close enough to the Object
void OnTriggerEnter(Collider other)
{
if(other.tag == "item")
{
objectOnRange = true;
item = other.gameObject;
}
}
void OnTriggerExit(Collider other)
{
if(other.tag == "item")
{
objectOnRange = false;
item = other.gameObject;
}
}
}