如何使一个对象跟随另一个对象?

时间:2019-07-10 00:12:38

标签: c# unity3d

如何在Unity中使佳能球对象跟随佳能?

看看我的屏幕截图:

screen shot

这是我关于佳能对象的代码:

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;

    }
}

0 个答案:

没有答案