unity中虚拟直升机、直升机的旋转移动,子弹的实例化爆炸等效果的实现
2015-02-02 00:18
796 查看
一、物体的实例化:设置预设物体
1、 首先把要做预设的物体预设
2、 创建一个空物体
3、 把实例化的脚本绑定在空物体上
4、 Targer目标物体绑定
5、 把空物体绑定在产生的位置
using UnityEngine;
using System.Collections;
public class ShiLiHua : MonoBehaviour
{
public GameObject Targer;
//public Transform zuobiao;
// 这样就可以直接拿到transform组件
void Start ()
{
//GameObject.Instantiate (Targer, zuobiao.position, zuobiao.rotation);
}
void Update(){
if(Input.GetKeyDown("space")){
//实例化函数Instantiate
GameObject.Instantiate (Targer, this.transform.position, this.transform.rotation);
}
}
}
直升机做成预支物体就可以导出,以备以后使用。
二、移动子弹:
void Update ()
{ //使飞机的子弹每秒向前移动多少的距离
this.transform.Translate (new Vector3 (0, 0, 1), Space.Self);
}
三、碰撞时产生爆炸的效果
void OnCollisionEnter (Collision other)
{
// 碰撞的第一个接触点
GameObject.Instantiate (Boom, other.contacts [0].point, this.transform.rotation);
Destroy (this.gameObject);
}
四、通过Input输入使飞机的螺旋桨旋转
public float r;
void Update () {
r = (Input.GetAxis ("Vertical"))*44;
this.transform.Rotate (r,0,0);
}
五、通过Input输入使飞机上下左右前后的移动并旋转:
using UnityEngine;
using System.Collections;
public class AddFoce_AirPlane : MonoBehaviour {
public float mg;
public float zuoyou;
public float shangxia;
public float qianhou;
public float xuanzhuan;
public Vector3 rotate_Air;
public Vector3 force;
public Vector3 forcezy;
public Vector3 qian;
public Vector3 zuo;
public GameObject Boom;
// Use this for initialization
void Start () {
mg = this.rigidbody.mass * 9.81f;
rotate_Air = new Vector3 (0,0,0);
}
void Update () {
zuoyou = Input.GetAxis ("Vertical");//zuoyou
shangxia = Input.GetAxis ("Horizontal1");//shangxia
qianhou = Input.GetAxis ("Horizontal");//qianhou
xuanzhuan = Input.GetAxis ("Vertical1");//xuanzhuan
qian = new Vector3 (this.transform.forward.x,0,this.transform.forward.z);//ba shi jie zuo biao zhong de Y gui 0;
zuo = new Vector3 (this.transform.right.x,0,this.transform.right.z);
rotate_Air.y += xuanzhuan;
rotate_Air.x = qianhou * 30;
force=qian*(30.5f * mg * Mathf.Tan (rotate_Air.x * Mathf.Deg2Rad));//jiao du zhuan hu du
rotate_Air.z = -1*(zuoyou * 30);
forcezy.x = 30.5f * mg * Mathf.Tan (rotate_Air.z * Mathf.Deg2Rad);//jiao du zhuan hu du
this.transform.rotation = Quaternion.Euler (rotate_Air);//fan hui yi ge xuan zhuan jiao du
}
void FixedUpdate(){
rigidbody.AddForce (0,mg,0);
rigidbody.AddForce (0,mg*shangxia*10,0);
rigidbody.AddForce (force);//tian jia yi ge xiang dui li shi feiji xuan zhuan zhihou yi ranxiang qianfei
rigidbody.AddForce (-forcezy);
}
1、 首先把要做预设的物体预设
2、 创建一个空物体
3、 把实例化的脚本绑定在空物体上
4、 Targer目标物体绑定
5、 把空物体绑定在产生的位置
using UnityEngine;
using System.Collections;
public class ShiLiHua : MonoBehaviour
{
public GameObject Targer;
//public Transform zuobiao;
// 这样就可以直接拿到transform组件
void Start ()
{
//GameObject.Instantiate (Targer, zuobiao.position, zuobiao.rotation);
}
void Update(){
if(Input.GetKeyDown("space")){
//实例化函数Instantiate
GameObject.Instantiate (Targer, this.transform.position, this.transform.rotation);
}
}
}
直升机做成预支物体就可以导出,以备以后使用。
二、移动子弹:
void Update ()
{ //使飞机的子弹每秒向前移动多少的距离
this.transform.Translate (new Vector3 (0, 0, 1), Space.Self);
}
三、碰撞时产生爆炸的效果
void OnCollisionEnter (Collision other)
{
// 碰撞的第一个接触点
GameObject.Instantiate (Boom, other.contacts [0].point, this.transform.rotation);
Destroy (this.gameObject);
}
四、通过Input输入使飞机的螺旋桨旋转
public float r;
void Update () {
r = (Input.GetAxis ("Vertical"))*44;
this.transform.Rotate (r,0,0);
}
五、通过Input输入使飞机上下左右前后的移动并旋转:
using UnityEngine;
using System.Collections;
public class AddFoce_AirPlane : MonoBehaviour {
public float mg;
public float zuoyou;
public float shangxia;
public float qianhou;
public float xuanzhuan;
public Vector3 rotate_Air;
public Vector3 force;
public Vector3 forcezy;
public Vector3 qian;
public Vector3 zuo;
public GameObject Boom;
// Use this for initialization
void Start () {
mg = this.rigidbody.mass * 9.81f;
rotate_Air = new Vector3 (0,0,0);
}
void Update () {
zuoyou = Input.GetAxis ("Vertical");//zuoyou
shangxia = Input.GetAxis ("Horizontal1");//shangxia
qianhou = Input.GetAxis ("Horizontal");//qianhou
xuanzhuan = Input.GetAxis ("Vertical1");//xuanzhuan
qian = new Vector3 (this.transform.forward.x,0,this.transform.forward.z);//ba shi jie zuo biao zhong de Y gui 0;
zuo = new Vector3 (this.transform.right.x,0,this.transform.right.z);
rotate_Air.y += xuanzhuan;
rotate_Air.x = qianhou * 30;
force=qian*(30.5f * mg * Mathf.Tan (rotate_Air.x * Mathf.Deg2Rad));//jiao du zhuan hu du
rotate_Air.z = -1*(zuoyou * 30);
forcezy.x = 30.5f * mg * Mathf.Tan (rotate_Air.z * Mathf.Deg2Rad);//jiao du zhuan hu du
this.transform.rotation = Quaternion.Euler (rotate_Air);//fan hui yi ge xuan zhuan jiao du
}
void FixedUpdate(){
rigidbody.AddForce (0,mg,0);
rigidbody.AddForce (0,mg*shangxia*10,0);
rigidbody.AddForce (force);//tian jia yi ge xiang dui li shi feiji xuan zhuan zhihou yi ranxiang qianfei
rigidbody.AddForce (-forcezy);
}
相关文章推荐
- Unity——Rigidbody实现真实的物体旋转效果/解决物体添加刚体后不随父物体移动/hingejoint
- 飞机游戏:敌机子弹的实现与死亡之后的爆炸效果
- 【Java_项目篇<1>】--JAVA实现坦克大战游戏--子弹连发+爆炸效果(四)
- Unity 移动端实现pad 端acceleration 控制移动、旋转
- Unity入门系列||Unity中如何用脚本实现物体的循环移动和旋转
- Unity中使用Rigdbody来给物体添加真是的移动旋转效果
- Unity 中实现子弹时间效果
- 我的Unity(3)一点一滴 实现物体移动和旋转的方法总结(1)
- Android基础-简单的动画实现 *补间动画 *透明度渐变 *旋转动画 *位移动画 *如果需要几个效果整合到一起,需要将所有效果全部放到set里
- unity_效果实现_移动按钮UI界面的交互实现(单位向量)
- 手势检测实现相册的左右滑动(并加上移动与旋转的特效) 首先,activity_main.xml里,使用ViewFlipper组件(可使用动画控制多个组件之间的切换效果) <?xml version="
- css3-transform-matrix 即矩阵实现物体旋转、缩放、移动效果
- 【Unity】代码实现视野的旋转和视野的拉近和拉远的效果
- iOS用三个手势和仿射变换实现图片的旋转缩放移动效果(类iBooks教科书)
- Javascript--利用向量旋转实现网页图片移动和碰撞反弹效果
- Unity2D - 4. 实现android虚拟摇杆控制人物移动
- 使用WPF Resource以及Transform等技术实现鼠标控制图片缩放和移动的效果
- 简单实现谷歌地图的图片缩放,移动效果的JS
- 旋转效果实现
- 使用js实现GridView单选效果自动设置交替行、选中行、鼠标移动行背景色