2010-08-19 49 views
0

如何在Unity3D中单击鼠标时创建对象的副本?创建一个游戏对象的副本

另外,如何在运行时选择要克隆的对象? (优选小鼠选择)。

+0

您可能会在gamedev上获得更好的回复(仍处于测试阶段)http://gamedev.stackexchange.com/有很多统一用户在那里 – 2010-08-23 16:51:43

+0

谢谢,我会尝试。 – ssuppal 2010-08-24 13:55:43

+0

或在unityanswers,一个SO专门为团结:http://answers.unity3d.com/ – luqui 2010-11-12 00:40:10

回答

4
function Update() { 

    var hit : RaycastHit = new RaycastHit(); 
    var cameraRay : Ray = Camera.main.ScreenPointToRay(Input.mousePosition); 

    if (Physics.Raycast (cameraRay.origin,cameraRay.direction,hit, 1000)) { 
     var cursorOn = true; 
    } 

    var mouseReleased : boolean = false; 

    //BOMB DROPPING 
    if (Input.GetMouseButtonDown(0)) { 

     drop = Instantiate(bomb, transform.position, Quaternion.identity); 
     drop.transform.position = hit.point; 

     Resize(); 

    } 
} 

function Resize() { 
    if (!Input.GetMouseButtonUp(0)) { 
      drop.transform.localScale += Vector3(Time.deltaTime, Time.deltaTime, 
               Time.deltaTime); 
      timeD +=Time.deltaTime; 
    } 
} 

你会想这比当然,许多电话来更新发生:

function Update() { 
    if(Input.GetMouseButton(0)) { 
     // This means the left mouse button is currently down, 
     // so we'll augment the scale    
     drop.transform.localScale += Vector3(Time.deltaTime, Time.deltaTime, 
              Time.deltaTime); 
    } 
} 
0

(在C#),最简单的方法是这样的:

[RequireComponent(typeof(Collider))] 
public class Cloneable : MonoBehaviour { 
    public Vector3 spawnPoint = Vector3.zero; 

    /* create a copy of this object at the specified spawn point with no rotation */ 
    public void OnMouseDown() { 
     Object.Instantiate(gameObject, spawnPoint, Quaternion.identity); 
    } 
} 

(第一行只是确保有一个对象连接到对象,它需要检测鼠标点击)

该脚本应该按原样运行,但我还没有测试过,如果没有,我会修复它。

0

如果你的脚本被附加到游戏物体(比如球体),那么你可以这样做:

public class ObjectMaker : MonoBehaviour 
{ 
    public GameObject thing2bInstantiated; // This you assign in the inspector 

    void OnMouseDown() 
    { 
     Instantiate(thing2bInstantiated, transform.position, transform.rotation); 
    } 
} 

你给实例化()三个参数:什么对象,什么位置,它是如何旋转。

这个脚本是做什么的,它实例化了一个确切位置&这个脚本附加到GameObject的旋转。通常情况下,您需要从GameObject中移除碰撞器,如果存在刚体,则需要移除碰撞器。你可以通过不同的方式来实例化事物,所以如果这个不适合你,我可以提供一个不同的例子。 :)