【发布时间】:2019-05-12 20:21:22
【问题描述】:
目标:
我正在创建一个冰球射手,射手每 3 秒生成一个冰球并向球门射门区域内的目标点开火;射门区域是横跨球门表面的平面,作为射门区域,因为并非所有射门都会在网上 - 冰球射手在该射门区域内生成一个随机点,这就是它获得目标 X、Y 和 Z 的方式.
我查看了移动对象的不同方式,例如 transform.forward、transform.up 等,但我认为我正在寻找的是 puckRB.MovePosition?
据我了解,将“Is Kinematic”设置为 true 时,MovePosition 方法会在冰球在环境中移动时将物理添加到冰球中,否则它只会传送它。我只是不确定如何将目标坐标与它需要的其他东西一起放入其中?
void GetTarget()
{
float scale = 1f;
// Grabs the aiming plane
Transform aimingPlane = GameObject.Find("AimingPlane").gameObject.transform.GetChild(1);
// Gets the moving X & Y area of the plane
float moveAreaX = aimingPlane.GetComponent<Renderer>().bounds.size.x / 2;
float moveAreaY = aimingPlane.GetComponent<Renderer>().bounds.size.y / 2;
// Generates a random value within the move area (X, Y)
float ranX = Random.Range(-moveAreaX * scale, moveAreaX * scale);
float ranY = Random.Range(-moveAreaY * scale, moveAreaY * scale);
// Grabs the center of the AimingPlane
Vector3 center = aimingPlane.GetComponent<Renderer>().bounds.center;
// Gets the targets coordinates (X and Y)
targetCoordsX = ranX + center.x;
targetCoordsY = ranY + center.y;
// Grabs AimingDot and places at target coordinates
Transform aimingDot = GameObject.Find("AimingPlane").gameObject.transform.GetChild(0);
aimingDot.position = new Vector3(targetCoordsX, targetCoordsY, center.z);
}
void FirePuck()
{
GameObject puckCopy = Instantiate(puck, shotPos.transform.position, shotPos.transform.rotation) as GameObject;
puckRB = puckCopy.GetComponent<Rigidbody>();
Vector3 test = new Vector3(targetCoordsX, targetCoordsY);
puckRB.MovePosition(test * Time.deltaTime);
}
总而言之,到目前为止,程序在射击区域内生成了一个随机目标,每 3 秒产生一个新的冰球,我现在只是试图从射手那里得到一个冰球到那个目标;冰球目前所做的只是传送到射手面前的一个点。
【问题讨论】:
标签: unity3d