当前位置:网站首页>unity常见的问题(一)
unity常见的问题(一)
2022-04-23 12:46:00 【一个不会码代码的小恐龙】
1.修改物体位置
无法直接修改物体的position
//transform.position.y = 10f; 错误的写法
//修正
Vector3 pos = transform.position;
transform.position = new Vector3(pos.x,20f,pos.z);
2.物体相互碰撞两方必须都要Collider 其中一方还要有 Rigidbody
3. target.position - transform.position 得到的不仅有方向还有大小
public class Notest : MonoBehaviour
{
public float speed= 5f;
public Transform target;
void Update()
{
//注意这不是单位向量 target.position - transform.position
transform.position = transform.position + (target.position - transform.position) * speed * Time.deltaTime;
//(target.position - transform.position).normalized 归一化处理
transform.position = transform.position + (target.position - transform.position).normalized * speed * Time.deltaTime;
//改进:在update中每一帧都要计算一次位置,上述出现向量和数进行了两次运算。加上小括号,变为向量和数只进行了一次运算 减少了运算量
transform.position = transform.position + (target.position - transform.position).normalized * (speed * Time.deltaTime);
}
}
版权声明
本文为[一个不会码代码的小恐龙]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_52021450/article/details/124348147
边栏推荐
猜你喜欢
[csnote] ER diagram
One way ANOVA of SPSS
Use source insight to view and edit source code
STM32控制步进电机(ULN2003+28byj)
在线计算过往日期天数,计算活了多少天
S2-062 远程命令执行漏洞复现(cve-2021-31805)
SSM framework series - annotation development day2-2
Metalama简介4.使用Fabric操作项目或命名空间
How do traditional enterprises cope with digital transformation? These books give you the answer
[daily question] chessboard question
随机推荐
One way ANOVA of SPSS
MySQL function - recursive function
SSM框架系列——数据源配置day2-1
A graphic designer's fantasy world | ones characters
How much does software testing help reduce program bugs?
SQLserver怎么插入或更新当天的星期数,bit而不是文本
使用Source Insight查看编辑源代码
leetcode:437. 路径总和 III【dfs 选还是不选?】
Object. The disorder of key value array after keys
力扣刷题之完全二叉树的节点个数
STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
Buuctf Web [bjdctf2020] zjctf, but so
Plato farm - a game of farm metauniverse with Plato as the goal
网站首页文件被攻击篡改的形式有哪些
[csnote] ER diagram
SSM框架系列——Junit单元测试优化day2-3
Number of nodes of complete binary tree
Source code analysis of synchronousqueue
【vulnhub靶场】-dc2
没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版