老师,为什么针不跟着小球转啊,我感觉和老师的代码没啥区别
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pin : MonoBehaviour {
private bool isFly = false;
private bool isReach = false;
private Transform startPoint;
public float speed = 5;
private Transform circle;
private Vector3 targetCirclePos;
// Use this for initialization
void Start () {
startPoint = GameObject.Find("StartPoint").transform;
circle = GameObject.Find("Circle").transform;
targetCirclePos = circle.position;
targetCirclePos.y -= 1.55f;
}
// Update is called once per frame
void Update()
{
if (isFly == false)
{
if (isReach == false)
{
transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, startPoint.position) < 0.05f)
{
isReach = true;
}
}
}
else
{
transform.position = Vector3.MoveTowards(transform.position, targetCirclePos, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, circle.position) < 0.05f)
{
transform.position = targetCirclePos;
transform.parent = circle;
isFly = false;
}
}
}
public void StartFly() {
isFly = true;
isReach = true;
}
}
同学你好,同学运行时看一下针是否是设置球为父物体,因为只有这段代码,老师看不见同学的StartFly方法是否调用了, 如果没调用,它就不会走else里边的,这样肯定就不会跟着球一起转了。
在pin的里这个地方应该这样写: if (Vector3.Distance(transform.position, targetCirclePos) < 0.05f)
{
Debug.Log(1);
transform.position = targetCirclePos;
transform.parent = circle;
isFly = false;
}
老师让同学debug一下看方法执行没,它确实没有执行,然后同学这里写的是circle.position,要细心耐心哦