UNITY 移动到指定位置的写法


//move towards a target at a set speed.
private void MoveTowardsTarget() {
    //the speed, in units per second, we want to move towards the target
    float speed = 1;
    //move towards the center of the world (or where ever you like)
    Vector3 targetPosition = new Vector3(0,0,0);

    Vector3 currentPosition = this.transform.position;
    //first, check to see if we're close enough to the target
    if(Vector3.Distance(currentPosition, targetPosition) > .1f) { 
        Vector3 directionOfTravel = targetPosition - currentPosition;
        //now normalize the direction, since we only want the direction information
        directionOfTravel.Normalize();
        //scale the movement on each axis by the directionOfTravel vector components

        this.transform.Translate(
            (directionOfTravel.x * speed * Time.deltaTime),
            (directionOfTravel.y * speed * Time.deltaTime),
            (directionOfTravel.z * speed * Time.deltaTime),
            Space.World);
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM