using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour{
public Canvas CanvasObj;//Canvas物体
public RectTransform SteeringWheelObj;//方向盘物体
private bool isFirstClick = true;//判断第一次按下
private Vector3 NowPos; //记录当前位置
private bool isClockwise; //判断顺时针
private float RoundSum = 0; //旋转总和
void Update(){
if (Input.GetMouseButton(0)){
Vector2 pos1;
//获取鼠标点击位置
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(CanvasObj.transform as RectTransform, Input.mousePosition, CanvasObj.worldCamera, out pos1))
{
pos1.x = pos1.x - SteeringWheelObj.position.x;
pos1.y = pos1.y - SteeringWheelObj.position.y;
Vector3 CenterPos = new Vector3(pos1.x, pos1.y, 0);
//判断第一次按下并记录
if (isFirstClick){
NowPos = CenterPos;
isFirstClick = false;
}
//记录位置判断方向
Vector3 movePos = Vector3.Cross(CenterPos, NowPos);
//判断顺时针与逆时针
if (movePos.z > 0) isClockwise = true;
else if (movePos.z < 0) isClockwise = false;
if (NowPos != CenterPos){
if(isClockwise){
if (RoundSum <= 540){//限制旋转角度总和
RoundSum += Vector3.Angle(NowPos, CenterPos);
transform.Rotate(new Vector3(0, 0, -Vector3.Angle(NowPos, CenterPos)));
}
}else{
if (RoundSum >= -540){//限制旋转角度总和
RoundSum -= Vector3.Angle(NowPos, CenterPos);
transform.Rotate(new Vector3(0, 0, Vector3.Angle(NowPos, CenterPos)));
}
}
}
NowPos = CenterPos;
}
}
if (Input.GetMouseButtonUp(0)) isFirstClick = true;
}
}
- 本文固定链接: http://www.nihao6.cn/archives/452
- 转载请注明: 虚拟世界 于 Unity3D虚拟世界 发表
捐 赠如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝或者微信直接向我捐款,在此非常感谢您对虚拟世界的捐赠。