当前位置:网站首页>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);
}
}边栏推荐
- Summer Ending
- There is a problem with the date when MySQL imports and exports data to excel
- Django framework - caching, signaling, cross site request forgery, cross domain issues, cookie session token
- Regular match the phone number and replace the fourth to seventh digits of the phone number with****
- Openstack learning notes -grace component insight
- Openstack learning notes (II)
- 1024水文
- OpenStack学习笔记(一)
- API in Nova
- JVM参数解释
猜你喜欢

Custom vertical table

关于一道教材题的讲解

Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream

初始c语言的知识2.0

剑指 Offer II 029. 排序的循环链表

Some knowledge about structure, enumeration and union

Analyse de l'optimisation de la réécriture des requêtes lazyagg de l'entrepôt

Sword finger offer II 025 Adding two numbers in a linked list

中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队

指针,它那些不得不说的题目
随机推荐
Conway's law can not be flexibly applied as an architect?
解析数仓lazyagg查询重写优化
Django framework - caching, signaling, cross site request forgery, cross domain issues, cookie session token
Golang keyboard input statement scanln scanf code example
Sword finger offer day 3 string (simple)
Germany holds global food security Solidarity Conference
剑指Offer 第 2 天链表(简单)
And console Log say goodbye
Component: is to switch between multiple components
用include what you use拯救混乱的头文件
Intercept based on byte length
Nova中的api
Shenzhen mintai'an intelligent second side_ The first offer of autumn recruitment
[machine learning] parameter learning and gradient descent
An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down
1024水文
Django框架——缓存、信号、跨站请求伪造、 跨域问题、cookie-session-token
中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队
关于数据在内存中存储的相关例题
Custom vertical table