unity3d 《见缝插针》----让“针”飞行

一.为将要飞出去的“针”添加一个脚本

脚本代码为:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class launch : MonoBehaviour {
    public int Speed = 20;
    private Rigidbody2D rb;
	// Use this for initialization
	void Start () {
        rb = GetComponent<Rigidbody2D>();
        rb.velocity = Vector2.up * Speed;
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

二、把之前拥有“飞行”功能的sprit放入库,并在scene上确立发射点,发射点再添加脚本,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour
{
    public GameObject spree;
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            spawPin();
        }
    }
    void spawPin()
    {
        Instantiate(spree, transform.position, transform.rotation);
    }
}

说明

1.GetCommponent()函数

此函数隶属于Component对象,用于返回一个类型的组件,如果没有,则返回null。

2.input

input是用来获取系统输入的接口。

3.input.GetButtonDown(“buttonName”)

返回一个布尔值,在帧运行期间,通过按钮的标识名,返回用户是否按下虚拟按钮。
需要在Update()内调用,每次Update()会刷新返回的布尔值

4.Instantiate()函数

public static T Instantiate<T>(T original) where T : Object;
public static T Instantiate<T>(T original, Transform parent) where T : Object;
public static T Instantiate<T>(T original, Vector3 position, Quaternion rotation) where T : Object;
public static T Instantiate<T>(T original, Transform parent, bool worldPositionStays) where T : Object;
public static T Instantiate<T>(T original, Vector3 position, Quaternion rotation, Transform parent) where T : Object;

UnityEngine.Object对象的instantiate()函数,表示实例化一个对象,常用于复制。

参数分别为:需实例化的对象,位置坐标,旋转坐标,实例化后是否为世界坐标,实例化后的父类。

一个组件或gameObje被克隆后,它的属性设置也会被克隆。

新对象默认的父节点是空的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值