创建游戏对象的副本

发布于 2024-09-15 08:42:34 字数 71 浏览 7 评论 0原文

在 Unity3D 中如何通过单击鼠标创建对象的副本?

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

How do you create a copy of an object upon mouse click in Unity3D?

Also, how could I select the object to be cloned during run-time? (mouse selection preferable).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热鲨 2024-09-22 08:42:34
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;
     }
}

您会希望在多次调用 Update 的过程中发生这种情况:

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

And you'll want this to happen over course of many calls to Update:

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);
    }
}
终止放荡 2024-09-22 08:42:34

最简单的方法(在 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);
    }
}

第一行只是确保对象上附加了一个碰撞器,需要检测鼠标单击)

该脚本应该按原样工作,但我还没有测试过还没有,如果没有的话我会修复它。

The simplest way (in c#) would be something like this:

[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);
    }
}

(The first line just makes sure there is a collider attached to the object, it's required to detect the mouse click)

That script should work as is, but I haven't tested it yet, I'll fix it if it doesn't.

我乃一代侩神 2024-09-22 08:42:34

如果你的脚本附加到一个游戏对象(例如,一个球体),那么你可以这样做:

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

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

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

该脚本的作用是在确切的位置和位置实例化某些内容。该脚本所附加的游戏对象的旋转。通常,您需要从游戏对象中删除对撞机,以及刚体(如果有)。实例化事物的方法有多种,因此,如果这个方法不适合您,我可以提供一个不同的示例。 :)

If your script is attached to a GameObject (say, a sphere), then you can do this:

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

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

You give Instantiate( ) three parameters: what object, what position, how is it rotated.

What this script does is it instantiates something at the exact position & rotation of the GameObject this script is attached to. Oftentimes you will need to remove the collider from the GameObject, and the rigidbody if there is one. There a variations in ways you can go about instantiating things, so if this one doesn't work for you I can provide a different example. : )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文