滑动脚本

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;  //控制玩家朝向
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值