Unity3D学习笔记(四):物理系统碰撞和预制体

----Constraints:约束(冻结)某一根轴,或者位移,或者旋转,冻结偏移度


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollisionTest : MonoBehaviour {
//检测碰撞
//Collision是类,是发生碰撞信息的集合(另一个物体的信息)
//两个物体,发生接触的时候
private void OnCollisionEnter(Collision collision)
{
Debug.Log("我进来了!");
Debug.Log(collision.gameObject.name);
}
//两个物体,持续接触的时候
private void OnCollisionStay(Collision collision)
{
Debug.Log("我在里面了!");
}
//两个物体,发生分离的时候
private void OnCollisionExit(Collision collision)
{
Debug.Log("我出来了!");
}
//触发器检测
//Collider是碰撞器的引用,包含于collision
//触发器和触发器或碰撞器,发生接触的时候
private void OnTriggerEnter(Collider other)
{
Debug.Log("触发了!");
}
private void OnTriggerStay(Collider other)
{ }
private void OnTriggerExit(Collider other)
{ }
}
Unity复习

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallTest : MonoBehaviour {
Rigidbody rig;
private void Awake()
{
rig = GetComponent<Rigidbody>();
}
// Use this for initialization
void Start()
{
// 可以碰撞的 (运动学干刚体和非运动学刚体)
// 冻结的时候 刚体会进入休眠吗? 不会
// 如果不冻结的状态 经过几秒种以后 就会进入休眠状态
}
private void OnGUI()
{
GUILayout.Label("Sleep :" + rig.IsSleeping());
}
void OnCollisionEnter(Collision collision)
{
Debug.Log("有碰撞到");
}
// Update is called once per frame
void Update () {
rig.WakeUp(); // 唤醒 }
}
子弹销毁:延时销毁,集中销毁
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletMove : MonoBehaviour {
public float moveSpeed = ;
// Use this for initialization
void Start()
{
Destroy(gameObject, );
}
// Update is called once per frame
void Update () { }
void FixedUpdate()
{
transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Enemy")
{
// 敌人掉血
EnemyInfo enemyInfo = collision.gameObject.GetComponent<EnemyInfo>();
if(enemyInfo) enemyInfo.GetDamage(Random.Range(, ));
// 子弹销毁
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour {
// 子弹的预制体
public GameObject bulletPrefab; // 资源面板中的
// 生成的位置
public Transform firePos;
public float moveSpeed = ;
public float rotSpeed = ;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Space))
{
// 发射子弹 在某个位置 实例化一个子弹
GameObject bullet = Instantiate(bulletPrefab, firePos.position, firePos.rotation);
BulletMove bulletMove = bullet.GetComponent<BulletMove>();
bulletMove.moveSpeed = ;
} }
// 物理相关的 写在 FixedUpdate
private void FixedUpdate()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime * Input.GetAxis("Vertical"));
transform.Rotate(Vector3.up * rotSpeed * Time.deltaTime * Input.GetAxis("Horizontal"), Space.World);
}
}
敌人信息
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyInfo : MonoBehaviour {
public int hp = ;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () { }
public void GetDamage(int dmg)
{
hp -= dmg;
if(hp <= )
{
hp = ;
Destroy(gameObject);
}
}
}
敌人控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyControl : MonoBehaviour {
public float moveSpeed = ;
Transform playerTrans;
// Use this for initialization
void Start () {
GameObject player = GameObject.Find("Player");
if(player)
{
playerTrans = player.transform; // 如果没有找到主角 会报 空引用
}
} // Update is called once per frame
void Update () {
transform.LookAt(playerTrans);
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); }
}
敌人生成点:需要生成的预制体,需要生成的位置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnEnemy : MonoBehaviour {
// 需要生成的预制体
public GameObject enemyPrefab;
float spawnInterval;
float timeCount = ;
// Use this for initialization
void Start () {
spawnInterval = Random.Range(, );
} // Update is called once per frame
void Update () {
timeCount += Time.deltaTime;
if(timeCount >= spawnInterval)
{
Instantiate(enemyPrefab, transform.position, Quaternion.identity); // Quaternion.identity 表示没有旋转
spawnInterval = Random.Range(, );
timeCount = ;
} }
}
Unity3D学习笔记(四):物理系统碰撞和预制体的更多相关文章
- java之jvm学习笔记四(安全管理器)
java之jvm学习笔记四(安全管理器) 前面已经简述了java的安全模型的两个组成部分(类装载器,class文件校验器),接下来学习的是java安全模型的另外一个重要组成部分安全管理器. 安全管理器 ...
- javascript学习笔记(四) Number 数字类型
数字格式化方法toFixed().toExponential().toPrecision(),三个方法都四舍五入 toFixed() 方法指定小数位个数 toExponential() 方法 用科学 ...
- muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制
目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...
- kvm虚拟化学习笔记(四)之kvm虚拟机日常管理与配置
KVM虚拟化学习笔记系列文章列表----------------------------------------kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51 ...
- Linux学习笔记(四) vi编辑器
一.vi 编辑器 vi 编辑器 (Visual Interface) 是所有 Unix 及 Linux 系统下标准的编辑器,相当于 Windows 系统中的记事本 它有三种模式,分别是: Comman ...
- Linux学习笔记(七) 查询系统
1.查看命令 (1)man 可以使用 man 命令名称 命令查看某个命令的详细用法,其显示的内容如下: NAME:命令名称 SYNOPSIS:语法 DESCRIPTION:说明 OPTIONS:选项 ...
- MySql学习笔记四
MySql学习笔记四 5.3.数据类型 数值型 整型 小数 定点数 浮点数 字符型 较短的文本:char, varchar 较长的文本:text, blob(较长的二进制数据) 日期型 原则:所选择类 ...
- unity3d学习笔记(一) 第一人称视角实现和倒计时实现
unity3d学习笔记(一) 第一人称视角实现和倒计时实现 1. 第一人称视角 (1)让mainCamera和player(视角对象)同步在一起 因为我们的player是生成的,所以不能把mainCa ...
- ZooKeeper学习笔记四:使用ZooKeeper实现一个简单的分布式锁
作者:Grey 原文地址: ZooKeeper学习笔记四:使用ZooKeeper实现一个简单的分布式锁 前置知识 完成ZooKeeper集群搭建以及熟悉ZooKeeperAPI基本使用 需求 当多个进 ...
随机推荐
- Javascript核心对象
JavaScript的实现包括以下3个部分: 1)核心(ECMAScript):描述了JS的语法和基本对象. 2)文档对象模型 (DOM):处理网页内容的方法和接口 3)浏览器对象模型(BOM):与浏 ...
- 11.sklearn.preprocessing.LabelEncoder的作用
In [5]: from sklearn import preprocessing ...: le =preprocessing.LabelEncoder() ...: le.fit(["p ...
- 2.深度学习中的batch_size的理解
Batch_Size(批尺寸)是机器学习中一个重要参数,涉及诸多矛盾,下面逐一展开. 首先,为什么需要有 Batch_Size 这个参数? Batch 的选择,首先决定的是下降的方向.如果数据集比较小 ...
- 5分钟带你入门vuex(vue状态管理)
如果你之前使用过vue.js,你一定知道在vue中各个组件之间传值的痛苦,在vue中我们可以使用vuex来保存我们需要管理的状态值,值一旦被修改,所有引用该值的地方就会自动更新,那么接下来我们就来学习 ...
- plsql的sql窗口中文模糊查询没有作用
环境变量新增: NLS_LANG = AMERICAN_AMERICA.AL32UTF8
- [LeetCode] 728. Self Dividing Numbers_Easy tag: Math
A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...
- git客户端msysGit和TortoiseGit使用
windows下使用TortoiseGit代替Git命令行操作(参考http://www.cnblogs.com/candle806/p/4071656.html) 1.配置TortoiseGit与m ...
- 课堂练习Complex类
Complex类 #include<iostream> #include<cmath> using namespace std; class Complex { public: ...
- workerman定时任务使用
定时任务在有些场合很实用,像淘宝的自动确认收货就必须放在服务端进行,这时workeran的定时任务就派上用场了,它可以支持毫秒,crontab的粒度是一分钟 需要注意的是因为定时任务一直在执行,业 ...
- #C++初学记录(算法2)
A - Game 23 Polycarp plays "Game 23". Initially he has a number n and his goal is to trans ...