void Update() { timer += Time.deltaTime; if (timer < restTime) { return; } h = MyJoyStick.GetHorizontalAxisRaw(); //水平 v = MyJoyStick.GetVerticalAxisRaw(); //垂直 if (h != 0 && v != 0) { return; } //changePlayerFace(); if (Edge_Mode) { Edge_Move(); return; } Record_last_below_box(); if (!Can_be_controlled_check()) { timer = 0; return; } if (check_velocity()) { timer = 0; return; } //player下落时无法被控制 if (h != 0 || v != 0) { collider_self.enabled = false; RaycastHit hit; Ray ray = new Ray(transform.position, new Vector3(h, 0, v)); bool isHit = Physics.Raycast(ray, out hit, 1.1f);//检测移动时是否碰到物体 try { if (drag()) { timer = 0; collider_self.enabled = true; return; } //拖拽检测 }catch (NullReferenceException e) { } rigidbody_self.isKinematic = true; if (!isHit) { //Debug.Log("正常行走."); play_AudioSE(Normal_Move); RaycastHit player_down; player_down = ray_check(transform.position, new Vector3(0, -1, 0), 0.5f); if (player_down.transform == null) { return; } iTween.MoveTo(gameObject, iTween.Hash("x", transform.position.x + h, "z", transform.position.z + v, "easeType", "linear", "speed", speed)); //iTween.MoveTo(gameObject, iTween.Hash("x", h, "z", v, "easeType", "linear", "speed", speed)); } else { //前方有物体的情况 if (check_player_up()) { collider_self.enabled = true; timer = 0; return; } //正上方有物体时无法前进 if (check_player_forward_up(hit)) { //可以爬上box的情况 //向上前进时保持在站立物体的中心 //Debug.Log("前方有且只有一个物体"); targetpos = new Vector3(hit.transform.position.x, hit.transform.position.y + 1, hit.transform.position.z); //rigidbody.MovePosition(Vector3.Lerp(transform.position, targetpos, speed * Time.deltaTime)); play_AudioSE(Climbing_Move); iTween.MoveTo(gameObject, iTween.Hash("x", targetpos.x, "y", targetpos.y, "z", targetpos.z, "easeType", "linear", "speed", speed)); //iTween.MoveBy(gameObject, iTween.Hash("amount", targetpos-transform.position, "easeType", "linear", "speed", speed, "delay", .1)); } else { Debug.Log("前方的物体上有1个或多个物体"); } } collider_self.enabled = true; //Debug.Log("h=" + h + " v=" + v); } timer = 0; //if (m_Camera_transform != null) //{ // m_CamForward = Vector3.Scale(m_Camera_transform.forward, new Vector3(1, 0, 1)).normalized; // m_Move = m_CamForward * v + m_Camera_transform.right * h; //} }
bool drag() //拖动物体的实现 { if (Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.JoystickButton0) || MyJoyStick.JoyStick_Drag) //Xbox360_Button_A { rigidbody_self.isKinematic = true; drag_h = MyJoyStick.GetHorizontalAxisRaw(); drag_v = MyJoyStick.GetVerticalAxisRaw(); if (drag_h != 0 && drag_v != 0) { return(true); } RaycastHit hit; Ray ray = new Ray(transform.position, new Vector3(-drag_h, 0, -drag_v)); bool isHit = Physics.Raycast(ray, out hit, drag_distanse); RaycastHit hit_down; hit_down = ray_check(transform.position, new Vector3(0, -1, 0), 0.5f); if (hit_down.transform == null) { Debug.Log("玩家下方没有box,不能执行拖动"); return(true); } switch (isHit) { case true: //Debug.Log("向后拖动物体"); if (!drag_check()) { goto onlyPush; } //addHighLight(hit.rigidbody.gameObject); targetpos = new Vector3(transform.position.x + drag_h, transform.position.y, transform.position.z + drag_v); //iTween.MoveTo(gameObject, iTween.Hash("x", drag_h, "z", drag_v, "easeType", "linear", "speed", speed)); iTween.MoveBy(gameObject, iTween.Hash("amount", targetpos - transform.position, "easeType", "linear", "speed", speed, "delay", .1)); iTween.MoveBy(hit.transform.gameObject, iTween.Hash("x", drag_h, "z", drag_v, "easeType", "linear", "speed", speed, "delay", .2)); Debug.Log("通知" + hit.transform.gameObject.name); hit.transform.gameObject.GetComponent <BoxManager>().Affected_boxes.Notify_Affectedbox(1f); return(true); case false: //向前推动 onlyPush: //Debug.Log("向前推动物体"); ray = new Ray(transform.position, new Vector3(drag_h, 0, drag_v)); isHit = Physics.Raycast(ray, out hit, drag_distanse); //通知boxManager if (Notify_boxManager != null) { Notify_boxManager(this, new PlayerMoveDistance(new Vector3(drag_h, 0, drag_v), this.gameObject)); } else { Debug.Log("Notify_boxManager==null"); } //addHighLight(hit.rigidbody.gameObject); //targetpos = new Vector3(transform.position.x + drag_h, transform.position.y, // transform.position.z + drag_v); //rigidbody.MovePosition(Vector3.Lerp(transform.position, targetpos, smooth)); iTween.MoveBy(hit.transform.gameObject, iTween.Hash("x", drag_h, "z", drag_v, "easeType", "linear", "speed", speed, "delay", .1)); Debug.Log("player推动" + hit.transform.gameObject.name); hit.transform.gameObject.GetComponent <BoxManager>().Affected_boxes.Notify_Affectedbox(1f); return(true); } } return(false); }