UNity UGUI 2D图片追随问题

Unity中,GameObject追随问题的一种解决办法。针对UGUI的2D情况。

贴出代码

using UnityEngine;
using System.Collections;


public class JellyMove : MonoBehaviour {


    public Transform path;
   public  float speed;
	void Start () {




    }
	
	// Update is called once per frame
	void Update () {


    }
    void FixedUpdate()
    {
        transform.position = Vector3.MoveTowards(transform.position,path.position, speed * Time.deltaTime);
        Rigidbody2D rigidBody= transform.GetComponent();


        var lookPos = path.position - transform.position;
        float x = Vector2.Angle(lookPos,Vector2.right);
        float y = Vector2.Angle(lookPos, Vector2.up);
        Vector2 moveVec = new Vector2(x,y);


        if (lookPos.x < 0)
        {
            rigidBody.MoveRotation(y);
        }else
        {
            rigidBody.MoveRotation(-y);
        }
    }
}

Secne场景如下

UNity UGUI 2D图片追随问题_第1张图片

然后运行状态下,拖动被追随物体,就可以随便跑了,各个方向也是正确的

你可能感兴趣的:(Unity3D)