Unity之2d对象跟随鼠标旋转

public float turnSpeed = 3f;
// Update is called once per frame
void Update () {
	if(Input.GetMouseButton(0)){
		Vector3 worldPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,-Camera.main.transform.position.z));
		Vector3 direction = worldPos-transform.position;
		direction.z=0f;
		direction.Normalize();

		float targetAngle = Mathf.Atan2(direction.y,direction.x)*Mathf.Rad2Deg;
		transform.rotation = Quaternion.Slerp( transform.rotation, Quaternion.Euler( 0, 0, targetAngle ), turnSpeed * Time.deltaTime );
	}
}

你可能感兴趣的:(Unity)