微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

unity中物体移动到指定位置的四种方法

法一:使用Vector3.Movetowards()方法

void Update(){
    float step = Speed*Time.deltaTime();
    gameObject.transform.localPosition = Vector3.Movetowards(gameObject.transform.localPosition, new Vector3(0, 0, 60), step);
}

法二:使用iTween

iTween.Moveto(m_UIbgCamera, iTween.Hash("x", -10, "y", 0, "z", 50, "time", 5.0f,"islocal", true));  

法三:使用差值Lerp

void Update ()   
{  
    float step = Speed * Time.deltaTime;  
    gameObject.transform.localPosition=new Vector3(Mathf.Lerp(gameObject.transform.localPosition.x, 10, step),Mathf.Lerp(gameObject.transform.localPosition.y, 0, step),Mathf.Lerp(gameObject.transform.localPosition.z, 50, step));//插值算法
}  

法四:使用协程 IEnumerate(**注意:**调用该协程的方法是:StartCoroutine(MovetoPosition()); )

IEnumerator MovetoPosition(){     //方法的声明  必须使用IEnumerate关键字
        GameObject m_UIbgCamera = GameObject.Find("UI/FengMian/UIbgCamera");  
        while (m_UIbgCamera.transform.localPosition != new Vector3(-5, 0, 50)){  
            m_UIbgCamera.transform.localPosition = Vector3.Movetowards(m_UIbgCamera.transform.localPosition, new Vector3(-20, 0, 50), 10 * Time.deltaTime);  
            yield return 0;  
        }  
    }  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐