代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateToMouse : MonoBehaviour
{
public float speed;
private Vector2 direction;
private Vector2 direction2;
void Update()
{
FollowMouse();
}
void FollowMouse()
{
direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotateAngle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(rotateAngle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation,rotation,speed*Time.deltaTime);
}
void FollowMouse2()
{
direction2 = ((Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - new Vector2(transform.position.x, transform.position.y)).normalized;
transform.right = direction2;
}
}
|