Javascript 拾取和移动周围的对象

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;

我试着在c#中找到一个例子,把一个物体放到玩家的手的位置,然后四处移动。我在unity论坛上找到了一个例子,但是它在C#中如何实现呢

这是我找到的代码,但我需要在单击按钮时抓取它,播放器需要在对象前面

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