拾起并移动物体

时间:2017-08-30 15:08:40

标签: javascript c# unity3d

我试图在c#中找到任何一个例子来将一个物体挑选到玩家的手位置并四处移动。然后当释放按钮时,对象被删除。我在团结论坛中找到了一个例子,但它在javascript中,如何在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;
                 }

          }
      }
  }
  }

我已经试过这个c#示例,但它拖了一下。当我抓住主摄像头时,我需要找到将它放在空物体上的方法,我需要改为抓住插入播放器位置的空物体

using UnityEngine;
using System.Collections;

public class drag : MonoBehaviour {
float distance = 10;
void OnMouseDrag(){
    Vector3 mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance);
    Vector3 objPosition = Camera.main.ScreenToWorldPoint (mousePosition);
    transform.position = objPosition;
}
}

1 个答案:

答案 0 :(得分:1)

要实现你的想法,有必要结合很多东西。

  1. 用于检测玩家何时在对象旁边的触发器
  2. 从播放器读取输入以检查他/她是否已按下或释放 抓住对象的按钮
  3. 将GameObject的位置更改为 字符
  4. 抓住手中抓住的GameObject孩子。所以两者一起移动
  5. 此脚本解决了上述所有步骤。你需要注意添加刚体和对撞机。此外,您还需要将要收集的对象标记为&#34; item&#34;

    额外注意:如果您将收集的项目设置为另一个具有网格的GameObject的子项,例如手,则子项将更改其形状。因此,使用放置在手的位置的空GameObject。并将检查器中的Empty 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;
            }
        }
    
    }