效果图如下
代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowMouse : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
FollowMouseRotate();
FollowMouseMove();
}
//物体跟随鼠标旋转
private void FollowMouseRotate()
{
//获取鼠标的坐标,鼠标是屏幕坐标,Z轴为0,这里不做转换
Vector3 mouse = Input.mousePosition;
//获取物体坐标,物体坐标是世界坐标,将其转换成屏幕坐标,和鼠标一直
Vector3 obj = Camera.main.WorldToScreenPoint(transform.position);
//屏幕坐标向量相减,得到指向鼠标点的目标向量,即黄色线段
Vector3 direction = mouse - obj;
//将Z轴置0,保持在2D平面内
direction.