经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
Unity 2D 札记
来源:cnblogs  作者:ComputerPlayerJs  时间:2019/3/12 8:40:37  对本文有异议

1.效应器

Point Effector 2D: 点效应器。进入区域,吸引或排斥物体

 

Area Effector 2D: 区域效应器,可以用来做马里奥的管道移动效果

 

Surface Effector 2D :表面效应器。实现传送带效果

 

PlatForm Effector 2D:平台效应器。实现2D游戏里面的台阶效果

2.控制移动

  1. // Update is called once per frame
  2. void Update () {
  3. if (Input.GetKey(KeyCode.A))
  4. {
  5. MoveLeft();
  6. }
  7. if (Input.GetKey(KeyCode.D))
  8. {
  9. MoveRight();
  10. }
  11. if (Input.GetKeyDown(KeyCode.Space))
  12. {
  13. Jump();
  14. }
  15. }
  16. void MoveLeft()
  17. {
  18. var v = rgbd.velocity;
  19. v.x = -movespeed;
  20. rgbd.velocity = v;
  21. //gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.left * movespeed, ForceMode2D.Force);
  22. }
  23. void MoveRight()
  24. {
  25. var v = rgbd.velocity;
  26. v.x = movespeed;
  27. rgbd.velocity = v;
  28. //gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.right * movespeed, ForceMode2D.Force);
  29. }
  30. public void Jump()
  31. {
  32. if (isground)
  33. {
  34. gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpspeed, ForceMode2D.Impulse);
  35. isground = false;
  36. }
  37. }

3.敌人来回移动

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class laihui : MonoBehaviour {
  5. public float movespeed=3.0f;
  6. private Vector2 currentposition;
  7. public float distnation=10.0f;
  8. private int face = 1;
  9. // Use this for initialization
  10. void Start () {
  11. currentposition = gameObject.transform.position;
  12. }
  13. // Update is called once per frame
  14. void Update () {
  15. if (face == 1)
  16. {
  17. gameObject.transform.Translate(Vector2.right * movespeed * Time.deltaTime);
  18. }
  19. if (face == 0)
  20. {
  21. gameObject.transform.Translate(Vector2.left * movespeed * Time.deltaTime);
  22. }
  23. if (gameObject.transform.position.x > currentposition.x+distnation)
  24. {
  25. face = 0;
  26. }
  27. if (gameObject.transform.position.x < currentposition.x)
  28. {
  29. face = 1;
  30. }
  31. }
  32. }

 

原文链接:http://www.cnblogs.com/ComputerPlayerJs/p/10511784.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号