如何在Unity中使佳能球对象跟随佳能?
看看我的屏幕截图:
这是我关于佳能对象的代码:
public class canonTrajectory : MonoBehaviour
{
public Transform Cannon;
Vector3 cubeposition;
// public Transform Sphere;
public float followSharpness = 0.1f;
// Start is called before the first frame update
Vector3 _followOffset;
Vector3 sphereposition;
void Start()
{
// Cache the initial offset at time of load/spawn:
cubeposition = new Vector3(Cannon.transform.position.x, Cannon.transform.position.y, Cannon.transform.position.z);
sphereposition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
_followOffset = Cannon.transform.position - transform.position;
}
// Update is called once per frame
void Update()
{
sphereposition = _followOffset + Cannon.transform.position;
transform.position += sphereposition;
// Apply that offset to get a target position.
// Vector3 targetPosition = cubeposition + _followOffset;
// Keep our y position unchanged.
//targetPosition.y = transform.position.y;
// Smooth follow.
//transform.position += (targetPosition - transform.position) * followSharpness;
}
}