unity3D 物体跟随移动

//脚本挂载移动的物体上

public class Player : MonoBehaviour {
public float Speed = 4;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    float h = Input.GetAxis("Horizontal");
    transform.Translate(Vector3.right * h * Speed * Time.deltaTime, Space.World);

    float v = Input.GetAxis("Vertical");
    transform.Translate(Vector3.forward * v * Speed * Time.deltaTime, Space.World);
    
}

private void OnTriggerEnter(Collider other)
{
    switch (other.tag) {
        case "Sphere":
          other.GetComponent().enabled = true;
           // Destroy(gameObject);
            break;

        default:
            break;

    }
    
}

}
//Tag 换成Sphere
unity3D 物体跟随移动_第1张图片

//脚本挂载跟随的物体上
//打开脚本是跟随移动 关闭是触发移动
// public Transform target; 把移动的物体拖上

public class pet : MonoBehaviour {

//宝宝跟随的目标
public Transform target;
//宝宝跟随目标的偏移量
public Vector3 offset;
// Use this for initialization
void Start()
{

}
// Update is called once per frame
void Update()
{
   // Debug.Log(Vector3.Lerp(new Vector3(0, 0, 0), new Vector3(0, 0, 10), Time.deltaTime));
}
void LateUpdate()
{
    offset = target.forward * -2 + target.up * 2;
    transform.position = Vector3.Lerp(transform.position, target.position + offset, Time.deltaTime);
    transform.rotation = target.rotation;
}

}

你可能感兴趣的:(unity)