Unity学习笔记 之 发射小球碰撞物体的代码记录

Unity学习笔记 之 发射小球碰撞物体的代码记录

大家好,又见面了,我是全栈君。

绑定在摄像机上的脚本

using UnityEngine;
using System.Collections;

public class abc : MonoBehaviour {

	//设置移动速度
	public int speed = 5;

	//设置将被初始化载入的对象
	public Transform newobject = null;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		//通过左右方向键,或A、D字母键控制水平方向。实现往左、往右移动
		float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
		//通过上下方向键,或W、S字母键控制垂直方向,实现往前、往后移动
		float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
		//移动 绑定物的 x、z 轴,即移动 摄像机的 x、z 轴。

transform.Translate(x,0,z); //推断是否按下鼠标的左键 if (Input.GetButtonDown(“Fire1”)) { //实例化命令:Instantiate(要生成的物体, 生成的位置, 生成物体的旋转角度) Transform n = (Transform)Instantiate(newobject, transform.position, transform.rotation); //转换方向 Vector3 fwd = transform.TransformDirection(Vector3.forward); //给物体加入力度 //Unity5之前的写法:n.rigidbody.AddForce(fwd * 2800); n.GetComponent<Rigidbody>().AddForce(fwd * 2800); } //推断是否按下字母button Q if (Input.GetKey(KeyCode.Q)) { //改变 绑定物的 y 轴,即改变 摄像机的 y 轴。 transform.Rotate(0,-25*Time.deltaTime,0,Space.Self); } //推断是否按下字母button E if (Input.GetKey(KeyCode.E)) { transform.Rotate(0,25*Time.deltaTime,0,Space.Self); } //推断是否按下字母button Z if (Input.GetKey(KeyCode.Z)) { //旋转 绑定物的 y 轴,即旋转 摄像机的 y 轴。 transform.Rotate(-25*Time.deltaTime,0,0,Space.Self); } //推断是否按下字母button X if (Input.GetKey(KeyCode.X)) { //旋转 绑定物的 y 轴,即旋转 摄像机的 y 轴。 transform.Rotate(25*Time.deltaTime,0,0,Space.Self); } //推断是否按下字母button F if (Input.GetKey(KeyCode.F)) { //移动 绑定物的 y 轴。即移动 摄像机的 y 轴。 transform.Translate(0,-5*Time.deltaTime,0); } //推断是否按下字母button C if (Input.GetKey(KeyCode.C)) { //移动 绑定物的 y 轴,即移动 摄像机的 y 轴。

transform.Translate(0,5*Time.deltaTime,0); } } }

绑定在发射的小球上的脚本

using UnityEngine;
using System.Collections;

public class xiaomie : MonoBehaviour {

	// Use this for initialization
	void Start () {
		//销毁物体,gameObject。目測应该是指物体自身。即达到自我销毁的需求.
		Destroy(gameObject, 3.0f);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108407.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号