private void TouchControl() {
if (state != STATE_DIALOG && state != STATE_DIE) { //如果角色不在对话状态/死亡状态,才能移动
int touchCount = 0;
if (touchCount < Input.touchCount) {
Vector2 touchPosition = Input.GetTouch(touchCount).position;
touchPosition.y = 480 - touchPosition.y;
//如果是单击或者是滑动
if (Input.GetMouseButtonDown(0) || Input.GetTouch(touchCount).phase == TouchPhase.Moved ) {
Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow); // 不放心的话,画出这条射线看看
RaycastHit hit; //用来从一个raycast后获取信息。
if (Physics.Raycast(ray, out hit)) { //如果射线发生碰撞返回true
float touchDist = (transform.position - hit.point).magnitude; //返回向量的长度
if (touchDist > 0.1) {
targetLocation = hit.point;
state = STATE_MOVING;
targetCircle.transform.position = hit.point;
}
}
}
}
}
// 以上代码,主要是获取一个目标点,有了目标点,我们就要让玩家行走移动了。
else if (state == STATE_MOVING) {
Vector3 movement = Vector3.zero;
movement = targetLocation - transform.position;
movement.y = 0;
float dist = movement.magnitude;
if (dist < 0.1) {
state = STATE_STAND;
}
else {
movement = movement.normalized * speed;
}
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
character.Move(movement);
FaceMovementDirection();
}
//控制角色朝向移动方向
private void FaceMovementDirection (){
Vector3 horizontalVelocity = character.velocity;
horizontalVelocity.y = 0; //忽略垂直移动
if ( horizontalVelocity.magnitude > 0.1f )
player.forward = horizontalVelocity.normalized; //控制玩家朝向
}
if (state != STATE_DIALOG && state != STATE_DIE) { //如果角色不在对话状态/死亡状态,才能移动
int touchCount = 0;
if (touchCount < Input.touchCount) {
Vector2 touchPosition = Input.GetTouch(touchCount).position;
touchPosition.y = 480 - touchPosition.y;
//如果是单击或者是滑动
if (Input.GetMouseButtonDown(0) || Input.GetTouch(touchCount).phase == TouchPhase.Moved ) {
Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow); // 不放心的话,画出这条射线看看
RaycastHit hit; //用来从一个raycast后获取信息。
if (Physics.Raycast(ray, out hit)) { //如果射线发生碰撞返回true
float touchDist = (transform.position - hit.point).magnitude; //返回向量的长度
if (touchDist > 0.1) {
targetLocation = hit.point;
state = STATE_MOVING;
targetCircle.transform.position = hit.point;
}
}
}
}
}
// 以上代码,主要是获取一个目标点,有了目标点,我们就要让玩家行走移动了。
else if (state == STATE_MOVING) {
Vector3 movement = Vector3.zero;
movement = targetLocation - transform.position;
movement.y = 0;
float dist = movement.magnitude;
if (dist < 0.1) {
state = STATE_STAND;
}
else {
movement = movement.normalized * speed;
}
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
character.Move(movement);
FaceMovementDirection();
}
//控制角色朝向移动方向
private void FaceMovementDirection (){
Vector3 horizontalVelocity = character.velocity;
horizontalVelocity.y = 0; //忽略垂直移动
if ( horizontalVelocity.magnitude > 0.1f )
player.forward = horizontalVelocity.normalized; //控制玩家朝向
}