经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
Unity相机跟随-----根据速度设置偏移量
来源:cnblogs  作者:小辉歌  时间:2019/3/4 8:43:32  对本文有异议

这里假设在水中的船,船有惯性,在不添加前进动力的情况下会继续移动,但是船身是可以360度自由旋转,当船的运动速度在船的前方的时候,相机会根据向前的速度的大小,设置相机的偏移量,从而提高游戏的动态带感。

但是由于惯性船的速度不一定在船的,因此可以获得当前船的速度方向在船的前方的投影分量,当分量与船的前方同向,那么设置偏移量为:速度分量的长度与船的最大比值t,乘以相机设定的最大偏移量

 

代码1

如下

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CameraControl : MonoBehaviour {
  5. GameObject player;
  6. public float speed=10f;

  7. 最大偏移量,这里最好能保证角色在屏幕范围内
  8. public float radius;// Use this for initialization
  9. void Start () {
  10. player = GameObject.FindWithTag("Player");
  11. }
  12. // Update is called once per frame
  13. void Update () {
  14. Follow();
  15. }void Follow()
  16. {
  17. if (!player)
  18. {
  19. player = GameObject.FindWithTag("Player");
  20. }
  21. else
  22. {
  23. Rigidbody2D rigid= player.GetComponent<Rigidbody2D>();
  24. Ship ship = player.GetComponent<Ship>();
  25. Vector3 playerVelocity = rigid.velocity;
  26. //求出角色的速度在角色的正方向的分量速度
  27. Vector3 playerVelocityOnForward= Vector3.Project(playerVelocity,player.transform.up);
  28. //得到分量速度的方向与正方向是否同符号
  29. float dot = Vector3.Dot(playerVelocityOnForward, player.transform.up);
  30. //如果同符号,那么得到它的模与最大速度值的比值,用该比值乘以radius,即可得到相机的偏移位置量;如果不同号,即速度方向相反,那么比例值设置为0
  31. float offsetLength = (dot >= 0 ? playerVelocityOnForward.magnitude / ship.maxSpeed : 0) * radius;
  32. transform.position = Vector3.Lerp (transform.position, player.transform.position + player.transform.up* offsetLength - Vector3.forward, Time.deltaTime * speed);
  33. }
  34. }
  35. }

 

在代码1上可以看出,在飞船开动引擎和关闭引擎时,由于速度的剧烈变化,相机也会跟着剧烈变化,玩家眼睛无法跟上画面的剧烈变化,最好的解决办法是将radius的变化放缓,从而使得相机的动作流畅,便有了代码2.

代码2:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CameraControl : MonoBehaviour {
  5. GameObject player;
  6. public float speed=10f;//相机的最大速度
  7.  
  8. public float radius;
  9. float minRadius=1f;
  10. float currentRadius;
  11. // Use this for initialization
  12. void Start () {
  13. player = GameObject.FindWithTag("Player");
  14. }
  15. // Update is called once per frame
  16. void Update () {
  17. Follow();
  18. }void Follow()
  19. {
  20. if (!player)
  21. {
  22. player = GameObject.FindWithTag("Player");
  23. }
  24. else
  25. {
  26. Rigidbody2D rigid= player.GetComponent<Rigidbody2D>();
  27. Ship ship = player.GetComponent<Ship>();
  28. Vector3 playerVelocity = rigid.velocity;
  29. //求出角色的速度在角色的正方向的分量速度
  30. Vector3 playerVelocityOnForward= Vector3.Project(playerVelocity,player.transform.up);
  31. //得到分量速度的方向与正方向是否同符号
  32. float dot = Vector3.Dot(playerVelocityOnForward, player.transform.up);
  33. //如果同符号,那么得到它的模与最大速度值的比值,用该比值乘以radius,即可得到相机的偏移位置量;如果不同号,即速度方向相反,那么比例值设置为0
  34. float offsetLength = (dot >= 0 ? playerVelocityOnForward.magnitude / ship.maxSpeed : 0) * radius;
  35. //Debug.Log(offsetLength);
  36. //如果按下了加速键,那么让相机在角色前方offsetLength处,否则,相机在角色前方最小偏移处
  37. if (Input.GetKey(KeyCode.UpArrow))
  38. {
  39. if (currentRadius <= radius)
  40. {
  41. //currentRadius = currentRadius + Time.deltaTime * 3f;
  42. //currentRadius = Mathf.Clamp(currentRadius, minRadius, radius);
  43. //currentRadius = offsetLength;
  44. currentRadius = Mathf.Lerp(currentRadius, offsetLength, Time.deltaTime * 3);
  45. };
  46. }
  47. else
  48. {
  49. if (currentRadius > minRadius)
  50. {
  51. currentRadius = currentRadius - Time.deltaTime * 3f;
  52. }
  53. currentRadius = Mathf.Clamp(currentRadius, minRadius, radius);
  54. }
  1. transform.position = Vector3.Lerp(transform.position, player.transform.position + player.transform.up * currentRadius - Vector3.forward, Time.deltaTime * speed);
  1. } } }

 

原文链接:http://www.cnblogs.com/xiaoahui/p/10463746.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号