这一次是我自己编程了一个物体的移动轨迹,我让物体在z轴上呈现直线移动,而在x轴上进行sin函数一样的移动,所以最后会呈现出物体走s形。
如何使用sin函数呢?
首先在最前面申明using System;
然后Math.Sin()进行调用就行了。
Math是一个包含多种数学工具的数学包:Sin,Cos,Sqrt,Pow等。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class SimpleLogic : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GameObject obj = this.gameObject;
Vector3 pos = obj.transform.localPosition;
pos.z += (float)0.01;
pos.x =(float)Math.Sin(pos.z);
obj.transform.localPosition = pos;
}
}
该文章介绍了一种编程方法,通过在Unity3D中应用Math.Sin函数,使游戏物体在z轴直线移动的同时,在x轴上按照正弦函数规律移动,从而创造出物体沿S形路径行进的效果。在C#脚本中,利用Update函数更新物体位置,实现动态的S形轨迹运动。

1331

被折叠的 条评论
为什么被折叠?



