当前位置:网站首页>Unity 2D游戏中的抛物运动
Unity 2D游戏中的抛物运动
2022-06-25 12:40:00 【九本才】

using UnityEngine;
public class ProjectileMotion : MonoBehaviour
{
//投掷物
[SerializeField] private GameObject bomb;
//力
[SerializeField] private float force;
[SerializeField] LineRenderer lineRenderer;
private float theta;
//速度
private float velocity;
Vector3[] ves;
private void Start()
{
lineRenderer.positionCount = 100;
ves = new Vector3[100];
}
Vector2 dir;
private void Update()
{
//弧度
float yLength = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
float xLength = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
//返回其切线为两个浮点数的商的角度。
theta = Mathf.Atan2(yLength, xLength);
dir = ((Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - (Vector2)transform.position).normalized;
//速度
velocity = (dir * force).magnitude;
if (Input.GetKeyDown(KeyCode.J))
{
GameObject Projectile = Instantiate<GameObject>(bomb, transform.position, Quaternion.identity);
Projectile.GetComponent<Rigidbody2D>().velocity = dir * force;
}
for (int i = 0; i < 100; i++)
{
float t = i * 0.05f;
float x = velocity * t * Mathf.Cos(theta) + transform.position.x;
float y = velocity * t * Mathf.Sin(theta) + 0.5f * Physics2D.gravity.y * t * t + transform.position.y;
ves[i] = new Vector3(x, y, 0);
}
lineRenderer.SetPositions(ves);
}
}边栏推荐
- It's an artifact to launch a website in a few minutes
- KVM 脚本管理 —— 筑梦之路
- leetcode:918. Maximum sum of circular subarray [reverse thinking + maximum subarray sum]
- Sword finger offer day 1 stack and queue (simple)
- 關於一道教材題的講解
- 中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队
- Maui's learning path (II) -- setting
- [pit avoidance means "difficult"] actionref current. Reload() does not take effect
- The starting point for learning programming.
- 关于猜数字游戏的实现
猜你喜欢
![[machine learning] model and cost function](/img/64/dc7677a3743eb8d060b7b1ecc9aa79.jpg)
[machine learning] model and cost function

一篇文章讲清楚MySQL的聚簇/联合/覆盖索引、回表、索引下推

Conway's law can not be flexibly applied as an architect?
Module 5 (microblog comments)

指针,它那些不得不说的题目

golang键盘输入语句scanln scanf代码示例

KDD 2022 | graphmae: self supervised mask map self encoder
![[turn] starting from the end, analyze in detail how to fill in the college entrance examination volunteer](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[turn] starting from the end, analyze in detail how to fill in the college entrance examination volunteer

解析数仓lazyagg查询重写优化

1251- client does not support authentication protocol MySQL error resolution
随机推荐
Serenvlt first met
始终保持疫情防控不放松 营造安全稳定的社会环境
[pit avoidance refers to "difficult"] halfcheckedkeys rendering problem in semi selected status of antd tree
Summer Ending
Openstack learning notes -nova component insight
Introduction to mongodb chapter 01 introduction to mongodb
关于猜数字游戏的实现
KVM script management - the road to dream
康威定律,作为架构师还不会灵活运用?
Qt鼠标跟踪
C # switch between Chinese and English input methods
C# 切换中英文输入法
leetcode - 384. Scramble array
Leetcode: Sword finger offer II 091 Painting house [2D DP]
[pit avoidance refers to "difficult"] antd cascader implements new customized functions
Configuring pytorch in win10 environment
And console Log say goodbye
leetcode - 384. 打乱数组
KVM 脚本管理 —— 筑梦之路
德国举行全球粮食安全团结会议