编程语言update is called once per frame什么鬼
发布网友
发布时间:2023-07-14 15:50
我来回答
共2个回答
热心网友
时间:2024-12-03 12:09
Unity3d人工智能确定性AI算法。
核心代码:
using UnityEngine;
using System.Collections;
public class PlayObject : MonoBehaviour
{
[HideInInspector]
public float moveVx;//x方向的分速度
[HideInInspector]
public float moveVy;//y方向的分速度
/// <summary>
/// 2维坐标(x,y)
/// </summary>
public Vector2 Position
{
get
{
return new Vector2(this.transform.position.x, this.transform.position.y);
}
}
private Vector2 _vHeading;
/// <summary>
/// //设置导弹的前进方向的归一化向量m_vHeading
/// </summary>
public Vector2 vHeading
{
get
{
float length = Mathf.Sqrt(moveVx * moveVx + moveVy * moveVy);
if (length != 0)
{
_vHeading.x = moveVx / length;
_vHeading.y = moveVy / length;
}
return _vHeading;
}
}
private Vector2 _vSide;
/// <summary>
/// 前进方向的垂直向量
/// </summary>
public Vector2 vSide
{
get
{
_vSide.x = -vHeading.y;
_vSide.y = vHeading.x;
return _vSide;
}
}
/// <summary>
/// 速度向量
/// </summary>
public Vector2 Velocity
{
get
{
return new Vector2(moveVx, moveVy);
}
}
/// <summary>
/// 速度标量
/// </summary>
public float Speed
{
get
{
return Mathf.Sqrt(moveVx * moveVx + moveVy * moveVy);
}
}
public float MaxSpeedRate;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// 移动物体
/// </summary>
/// <param name="speedRate">移动的速度率,一般为1</param>
/// <param name="isLookAtVelocityVector">是否要这是速度矢量与物体的朝向一致</param>
public void Move(float speedRate, bool isLookAtVelocityVector)
{
this.transform.position += new Vector3(moveVx * Time.deltaTime, moveVy * Time.deltaTime, 0) * speedRate;
// Debug.Log("x:" + m_postion.x + "y:" + m_postion.y);
//调整导弹的朝向是的它和速度矢量合成方向一样
if (isLookAtVelocityVector)
{
LookAtVelocityVector();
}
}
/// <summary>
/// 使得物体始终朝向矢量速度的方向
/// </summary>
void LookAtVelocityVector()
{
float zAngles = Mathf.Atan(moveVx / moveVy) * (-180 / Mathf.PI);
if (moveVy == 0)
{
zAngles = moveVx > 0 ? -90 : 90;
//跟以往的计算角度不同的是,这里加了moveVx==0的独立判断,这样可以在不控制的时候保持原状态
if (moveVx == 0)
{
zAngles = this.transform.rotation.eulerAngles.z;
}
}
if (moveVy < 0)
{
zAngles = zAngles - 180;
}
Vector3 tempAngles = new Vector3(0, 0, zAngles);
Quaternion tempQua = this.transform.rotation;
tempQua.eulerAngles = tempAngles;
this.transform.rotation = tempQua;
}
}
热心网友
时间:2024-12-03 12:10
update is called once per frame意思是游戏画面动作每帧更新一次
与之对应
Start is called before the first frame update在第一帧更新之前调用