41298人加入学习
(116人评价)
Unity零基础入门 - 打砖块(Unity 2017) - 旧版
价格 免费

sphere add rigidbody

//得到小球身上的刚体组件,使其获得初速度

 

 

GameObject.Instantiate(bullet,transform.position,transform.rotation);

Instantiate有返回值,表示我们实例化的子弹,GameObject b =GameObject.Instantiate(bullet,transform.position,transform.rotation);

将小球返回值设置为b

Rigibody rgd=b.GetComponent<Rigidbody>()得到小球身上的刚体组件

rgd.velocity=transform.forward *speed;

赋速度,速度包括方向和大小

按下鼠标左键实例化小球,获得该实例化小球。获得实例化小球的rigidbody。通过rgd.velocity赋予速度

获得实例化小球

获得实例化小球的刚体组件

对刚体组件赋予初速度

 

代码整体

using UnityEngine;

public GameObject bullet;

public speed=5;

void start(){

Debug.Log("Hello");

}

viod update(){

if(Input.GetMouseButtonDown(0)){

GameObject b=GameObject.Instantiate(bullet,stansform.position,transform.rotation);

Rigidbody rgd=b.GetComponent<Rigidbody>();

rgd.velocity=transform.forward*speed;

}

}

 

[展开全文]

刚体:

Rigidbody

创建并获取实例GameObject b = GameObject.Instantiate(bullet, transform.position, transform.rotation);

获取实例内的参数<刚体>
Rigidbody rgd =b.GetComponent<Rigidbody>();

修改刚体的参数<速度>rgd.velocity = transform.forward * speed;

[展开全文]

鼠标事件:

 

        鼠标事件,都是当鼠标和gui或者碰撞体(Collider)交互时候触发。需要说明的是drag其实就是鼠标down后up之前持续每帧都会发送此消息。

 

        OnMouseDown:当鼠标上的按钮被按下时触发的事件;

 

        OnMouseDrag:当用户鼠标拖拽GUI元素或碰撞体时调用;

 

        OnMouseEnter:当鼠标进入物体范围时被调用;

 

        OnMouseExit:当鼠标退出时被调用;

 

        OnMouseOver:当鼠标移动到某对象的上方时触发的事件;

 

        OnMouseUp:当鼠标按键被松开时触发的事件

 

按下事件:Input.GetMouseButtonDown()

  该方法只有一个参数,参数为0时,代表鼠标左键被按下,参数为1的时候,代表鼠标右键被按下,参数为2的时候代表鼠标中键被按下。 

void Update() {
        if (Input.GetMouseButtonDown(0))
            Debug.Log("Pressed left click.");

        if (Input.GetMouseButtonDown(1))
            Debug.Log("Pressed right click.");

        if (Input.GetMouseButtonDown(2))
            Debug.Log("Pressed middle click.");

    }
  抬起事件:Input.GetMouseButtonUp()

  鼠标在按下后肯定要抬起,按下的时候会触发按下事件,抬起的时候会触发抬起事件。与按下事件相同,抬起事件也只有一个参数,当参数为0的时候代表鼠标左键抬起,参数为1的时候代表鼠标右键被抬起,参数为2的时候代表鼠标中键被抬起。 

void Update() {
        if (Input.GetMouseButtonDown(0))
            Debug.Log("Pressed left click.");

        if (Input.GetMouseButtonDown(1))
            Debug.Log("Pressed right click.");

        if (Input.GetMouseButtonDown(2))
            Debug.Log("Pressed middle click.");

    }
  长按事件:Input.GetMouseButton()

  检测鼠标三个三个按键中某一按键一直按下的状态或者是获得按下的按键,和之前的两个事件一样,只有一个参数,当参数为0的时候代表鼠标左键长按,参数为1的时候代表鼠标右键长按,参数为2的时候代表鼠标中键被长按。

 

void Update() {
        if (Input.GetMouseButton(0))
            Debug.Log("Pressed left click.");

        if (Input.GetMouseButton(1))
            Debug.Log("Pressed right click.");

        if (Input.GetMouseButton(2))
            Debug.Log("Pressed middle click.");

    }

[展开全文]

1、forwar,z=1,x=0,y=0

2、velocity=方向*速度大小

[展开全文]

给子弹初速度

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour
{
    public GameObject bullet;//一定要确保bullet赋值了,在unity里面赋值
    public float speed = 5;  //这里定义在inspector面板很容易修改,以面板为准
    // Start is called before the first frame update
    void Start()
    {
       
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))//按下鼠标左键实例化
        {
          GameObject b=  GameObject.Instantiate(bullet, transform.position, transform.rotation);//实例化一个子弹
            Rigidbody rgd = b.GetComponent<Rigidbody>();//得到物体刚体组件
            rgd.velocity = transform.forward * speed;
        }
    }
}
[展开全文]

授课教师

SiKi学院老师

课程特色

图文(2)
下载资料(1)
视频(19)