Rigibody
先移动摄像机到想要的视角->GameObject->Align With View
可在点击Play时,初始便在自己想要的视角。
(例Cube1和Cube2)
点击Cube1->在Inspector界面点击Add Component->搜索Rigidbody并点击。
点击Play 物体受重力掉落。
停止Play->将Rigidbody界面打开->取消Use Gravity前的对号。
点击Play物体不会坠落。
此时Cube2同样取消Use Gravity前的对号。
点击Play,在Scene界面拖动Cube1装击Cube2,相互弹开。
编程(Cube1为例)
点击Cube1,在Inspector界面点击Add component,输入wow,按回车创建。点击Script后的框格,进入Microsoft Visual Studio界面,编辑如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class wow: MonoBehaviour{
//Use this for initialization
void Start() {
// 拿到rigid组件,拿到usegravity,复制bool
GetComponent<Rigidbody>().useGravity=false;
}
//Update is called once per frame
void Update() {
//keycode 由Event.keyCode返回的密钥代码。这些直接映射到键盘上的一个物理键。
//GetkeyDown 在框架期间返回true,用户开始按下按名称标识的键。
if (Input.GetKeyDown(KeyCode.S))
GetComponent<Rigidbody>().AddForce(Vector3.back*100);
if (Input.GetKeyDown(KeyCode.A))
GetComponent<Rigidbody>().AddForce(Vector3.left*100);
if (Input.GetKeyDown(KeyCode.D))
GetComponent<Rigidbody>().AddForce(Vector3.right*100);
if (Input.GetKeyDown(KeyCode.W))
GetComponent<Rigidbody>().AddForce(Vector3.forward*100);
}
}
点击Play,按wasd可控制Cube1,撞击在Cube2上,Cube2被撞飞。