弓箭运动轨迹的制作
2021/6/8 10:24:12
本文主要是介绍弓箭运动轨迹的制作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
using UnityEngine;
using System.Collections;
///
/// 弓箭轨迹模拟
/// 使用:直接挂载在一个物体上,物体就会像抛物线一样抛射出去
///
public class yundongguij : MonoBehaviour
{
public float Power = 10;//这个代表发射时的速度/力度等,可以通过此来模拟不同的力大小 public float Angle = 45;//发射的角度,这个就不用解释了吧 public float Gravity = -10;//这个代表重力加速度 public bool IsOne = false; private Vector3 MoveSpeed;//初速度向量 private Vector3 GritySpeed = Vector3.zero;//重力的速度向量,t时为0 private float dTime;//已经过去的时间 private Vector3 currentAngle; // Use this for initialization void Start() { //通过一个公式计算出初速度向量 //角度*力度 MoveSpeed = Quaternion.Euler(new Vector3(0, 0, Angle)) * Vector3.right * Power; currentAngle = Vector3.zero; } // Update is called once per frame void FixedUpdate() { //计算物体的重力速度 //v = at ; GritySpeed.y = Gravity * (dTime += Time.fixedDeltaTime); //Time.fixedDeltaTime 固定的时间增量 //位移模拟轨迹 transform.position += (MoveSpeed + GritySpeed) * Time.fixedDeltaTime; currentAngle.z = Mathf.Atan((MoveSpeed.y + GritySpeed.y) / MoveSpeed.x) * Mathf.Rad2Deg;//Mathf.Atan 求f的正切返回值 Mathf.Rad2Deg弧度到度的转换 transform.eulerAngles = currentAngle;//将currentAngle赋值给transform.eulerAngles(欧拉角) }
}
将脚本挂载到任意物体上即可实现物体呈抛物线形式运动
这篇关于弓箭运动轨迹的制作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)