又又一天的unity学习总结
2016-07-09 14:27
597 查看
~~~又看了一下午加晚上的教程,还是siki老师的坦克大战
做了下总结
1、通过刚体来施加力来使物体移动
先定义个初速度,用的GetAxis();这个的参数是轴,检测对应的轴,会返回值-1~1;默认是wasd和方向键都可以控制,再让刚体的
2、实现子弹
就是对子弹先进行设置,然后把子弹作为预制体,再创建一个空物体,在空物体上实例子弹就可以了。
并给一个初速度
3、Update与FixedUpdate的区别
FixedUpdate是会在每个固定的时间间隔被调用,FixeduPdate不受帧率的变化。Update是一帧一帧来的。
4、Transform.Find()
参数是一个对象的名字,这个可以通过写想要的对象的名字来获取,会返回对应的对象。
5、触发器发消息
collider.sendMessage();
可以在触发检测到了后给触发的对象发消息,也就是调用那个消息也就是函数的名字。
6、摄像头的视野跟随
对于两个物体的,可以以他们距离的中心来跟随。
先计算出现在设定好的摄像头与他们两个物体之间中心的距离,然后在此基础上,摄像头也就是去改变坐标。设置摄像头的视野大小。先找出比例,可以是两个物体之间的距离与现在摄像头视野大小做比例,然后让摄像头的视野大小一直是这个比例就可以了。
分享下各个脚本的代码
Tankovement
shiyegensui
TankAttack
TankHealth
Shell
还好C#不是很难,而且我这几天接触的都还是比较简单的。这几天unity就先学习到这,我也发现了unity真的很好用,但是api好多…….很多还是现用现百度谷歌把。这几天收获很大。
今天是乌云白帽大会啊,看会直播~~
做了下总结
1、通过刚体来施加力来使物体移动
先定义个初速度,用的GetAxis();这个的参数是轴,检测对应的轴,会返回值-1~1;默认是wasd和方向键都可以控制,再让刚体的
velocity = transform.forward*返回值*速度;transform.forward就是坐标前。前后的轴是Vertical 左右旋转的轴是 Horizontal 同样的。如果是两个相同的但是控制不同的,可以在轴那里复制个轴然后分别改名字。在edit中Project Setting 中Input ~。用GetAxis()来获取那个轴,返回值。根据坐标名称,返回虚拟坐标系中的值。
2、实现子弹
就是对子弹先进行设置,然后把子弹作为预制体,再创建一个空物体,在空物体上实例子弹就可以了。
GameObject.Instantiate(shellprefab,firePosition.position,firePosition.rotation)
并给一个初速度
go.GetComponent<Rigidbody>().velocity =go.transform.forward*shellSpeed;
3、Update与FixedUpdate的区别
FixedUpdate是会在每个固定的时间间隔被调用,FixeduPdate不受帧率的变化。Update是一帧一帧来的。
4、Transform.Find()
参数是一个对象的名字,这个可以通过写想要的对象的名字来获取,会返回对应的对象。
firePosition = transform.Find ("FirePosition");
5、触发器发消息
collider.sendMessage();
可以在触发检测到了后给触发的对象发消息,也就是调用那个消息也就是函数的名字。
collider.SendMessage("TankDamage");
void TankDamage(){ if (hp <= 0) return; hp -= Random.Range(10,20); if(hp<=0){ AudioSource.PlayClipAtPoint(siwang,transform.position); GameObject.Instantiate (TankSile, transform.position + Vector3.up, transform.rotation); Destroy (this.gameObject); }
6、摄像头的视野跟随
对于两个物体的,可以以他们距离的中心来跟随。
先计算出现在设定好的摄像头与他们两个物体之间中心的距离,然后在此基础上,摄像头也就是去改变坐标。设置摄像头的视野大小。先找出比例,可以是两个物体之间的距离与现在摄像头视野大小做比例,然后让摄像头的视野大小一直是这个比例就可以了。
public Transform play1; public Transform play2; private Vector3 cha; private Camera camera; // Use this for initialization void Start () { cha = transform.position-(play1.position+play2.position)/2; camera = this.GetComponent<Camera> (); } // Update is called once per frame void Update () { if (play1 == null || play2 == null) return; transform.position = cha+(play1.position+play2.position)/2; float distance = Vector3.Distance (play1.position,play2.position); float size = distance * 1.09f; camera.orthographicSize = size; }
分享下各个脚本的代码
Tankovement
public class Tankovement : MonoBehaviour { public float angularSpeed = 1; public float speed =2.0f; public Rigidbody rigidbody; public int number =1; public AudioSource audio; // Use this for initialization void Start () { rigidbody = this.GetComponent<Rigidbody> (); audio = this.GetComponent<AudioSource>(); } void FixedUpdate() { float v = Input.GetAxis ("VerticalPlayer"+number); rigidbody.velocity = transform.forward * v * speed; float h = Input.GetAxis ("HorizontalPlayer"+number); rigidbody.angularVelocity = transform.up * h * angularSpeed; } }
shiyegensui
public class shiyegensui : MonoBehaviour {
public Transform play1; public Transform play2; private Vector3 cha; private Camera camera; // Use this for initialization void Start () { cha = transform.position-(play1.position+play2.position)/2; camera = this.GetComponent<Camera> (); } // Update is called once per frame void Update () { if (play1 == null || play2 == null) return; transform.position = cha+(play1.position+play2.position)/2; float distance = Vector3.Distance (play1.position,play2.position); float size = distance * 1.09f; camera.orthographicSize = size; }
}
TankAttack
public class TankAttack : MonoBehaviour { private Transform firePosition; public GameObject shellprefab; public KeyCode fireKey = KeyCode.Space; public float shellSpeed=15; public AudioClip fashe; // Use this for initialization void Start () { firePosition = transform.Find ("FirePosition"); } // Update is called once per frame void Update () { if (Input.GetKeyDown (fireKey)) { AudioSource.PlayClipAtPoint(fashe,transform.position); GameObject go= GameObject.Instantiate(shellprefab,firePosition.position,firePosition.rotation)as GameObject ; go.GetComponent<Rigidbody>().velocity =go.transform.forward*shellSpeed; } } }
TankHealth
public class TankHealth : MonoBehaviour {
public float hp =100;
public GameObject TankSile;
public AudioClip siwang;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void TankDamage(){ if (hp <= 0) return; hp -= Random.Range(10,20); if(hp<=0){ AudioSource.PlayClipAtPoint(siwang,transform.position); GameObject.Instantiate (TankSile, transform.position + Vector3.up, transform.rotation); Destroy (this.gameObject); }
}
}
Shell
public class Shell : MonoBehaviour { public GameObject shellexprafab; private AudioSource audio; public AudioClip baozha; void Start(){ } public void OnTriggerEnter(Collider collider){ AudioSource.PlayClipAtPoint (baozha,transform.position); GameObject.Instantiate (shellexprafab,this.transform.position,this.transform.rotation); GameObject.Destroy (this.gameObject); if(collider.tag=="Tank"){ collider.SendMessage("TankDamage"); } } }
还好C#不是很难,而且我这几天接触的都还是比较简单的。这几天unity就先学习到这,我也发现了unity真的很好用,但是api好多…….很多还是现用现百度谷歌把。这几天收获很大。
今天是乌云白帽大会啊,看会直播~~
相关文章推荐
- Unity文件目录的读写
- Unity Shader入门精要读书笔记第二篇
- Unity 3D 学习笔记(1)
- Unity3d:UI面板管理整合进ToLua
- 「Unity」与iOS、Android平台的整合:2、导出的Android-Eclipse工程
- unity3d 5.0.2的安装与破解
- Unity中HDR外发光的使用
- Unity Shader实现跳动的心脏
- Unity5-ABSystem(一):AssetBundle原理
- Unity材质动画
- 【Unity优化】关于优化方面的整理
- Unity接入安卓程序,打包出错的解决办法
- Unity的Profiler性能分析
- Unity的Profiler性能分析
- unity3d中的坐标系
- Ubuntu 12.04在vmware10下无法进入Unity模式
- Unity3d使用心得(2):Unity3d 动态下载动画资源——AnimationClip 的使用
- Unity3D使用心得(1):ModelImporter的使用、在代码中添加动画片段。
- [Unity3D]查看与设置游戏帧数FPS
- unity自己制作动画(以image示例)