private void OnCollisionEnter(Collision collision) { if (!bMoveFlag && _ufpTransform) { RaycastHit _groundHit; if (Physics.Raycast(RayPointObj.transform.position, -Vector3.up, out _groundHit, GroundRayLength)) //悬空 { FPVector fpNormal = _ufpTransform.ChangeVec3ToTSVec(_groundHit.normal); if (_ufpTransform.up == fpNormal) { return; } FP angle = FPVector.Angle(_ufpTransform.up, fpNormal); FPVector fpPoint = _ufpTransform.ChangeVec3ToTSVec(_groundHit.point); FPVector newCrossVec3 = FPVector.Cross(_ufpTransform.up, fpNormal); if (_childObj) { FPVector forward = FPQuaternion.AngleAxis(angle, newCrossVec3) * (_ufpTransform.forward); FitSurfaceParent(forward, fpPoint, fpNormal); } } } }
public void Move() { if (bJumpFlag) { return; } FPVector groundNormal = FPVector.zero; RaycastHit hitInfo; if (!RayGroundInfo(out hitInfo)) { if (!bJumpFlag) { bMoveFlag = false; } return; //如果离地,直接返回 } else { groundNormal = new FPVector(hitInfo.normal.x, hitInfo.normal.y, hitInfo.normal.z); } if (!bMoveFlag) { bMoveFlag = true; } FPVector forward = _ufpTransform.forward; FPVector fpNormal = _ufpTransform.ChangeVec3ToTSVec(hitInfo.normal); RaycastHit barrierInfo; bool bExitBarrier = (RayBarrierInfo(_childObj.forward.ToVector(), out barrierInfo)); //表示前方检测到物体,暂时把检测到的物体都称之为障碍物 bool bSlope = false; //默认不是为斜坡标志 if (bExitBarrier) { hitInfo = barrierInfo; bSlope = (Vector3.Angle(Vector3.up, hitInfo.normal) > SlopeAngle) ? true : false; if (ForWardMode == E_ForWardMode.FM_ON_FORWARD) { forward = _ufpTransform.forward; } else { FP angle = 0; fpNormal = _ufpTransform.ChangeVec3ToTSVec(hitInfo.normal); if (groundNormal != FPVector.zero) { angle = FPVector.Angle(groundNormal, fpNormal); angle = -angle; } if (_childObj) { //Vector3 newCrossVec3 = Vector3.Cross(hitInfo.normal, transform.up); //forward = Quaternion.AngleAxis(angle, newCrossVec3) * (transform.forward); FPVector newCrossVec3 = FPVector.Cross(fpNormal, _ufpTransform.up); forward = FPQuaternion.AngleAxis(angle, newCrossVec3) * _ufpTransform.forward; } } } else { bSlope = ((FPVector.Angle(FPVector.up, fpNormal)) > SlopeAngle) ? true : false; } if (SurfaceMode == E_SurfaceMode.SM_ON_GROUND) //非跨面模式:供人型和乌贼模式下使用 { if (bExitBarrier && bSlope) { return; } } else if (SurfaceMode == E_SurfaceMode.SM_ON_SURFACE) //跨面模式:供乌贼下潜模式下使用 { if (bSlope) { MotionMode = E_MotionMode.MM_ON_LINE; bool flag = CanMoveByInk(ref hitInfo); //上墙后到边角的处理 ==>TODO flag = true; if (!flag) { FPVector position = _ufpTransform.ChangeVec3ToTSVec(hitInfo.point) + (_childObj.forward * MoveSpeed * 0.1f); FPVector currentPosition = _ufpTransform.position; //_ufpTransform.position = position; //transform.position = position.ToVector(); UpdatePosition(_ufpTransform.position); RayGroundInfo(out barrierInfo); flag = CanMoveByInk(ref barrierInfo); // transform.position = currentPosition.ToVector(); UpdatePosition(_ufpTransform.position); if (bExitBarrier || !flag) { return; } } } } if (hitInfo.transform != null && ForWardMode == E_ForWardMode.FM_ON_NORMAL) { FitSurfaceParent(forward, _ufpTransform.ChangeVec3ToTSVec(hitInfo.point), fpNormal); //调整贴面 } else if (hitInfo.transform != null && ForWardMode == E_ForWardMode.FM_ON_FORWARD) { if (transform.up != Vector3.up) { FP quaternionSpeed = 0.09f; FPQuaternion data = new FPQuaternion(0, _ufpTransform.rotation.y, 0, _ufpTransform.rotation.w); _ufpTransform.SetRotation(FPQuaternion.Slerp(_ufpTransform.rotation, data, quaternionSpeed)); } } if (MotionMode == E_MotionMode.MM_ON_ROUND) //画圆模式:按照圆的弧度来行走 { _destRoundData = RotationSpeed * MoveSpeed * FrameSyncManager.DeltaTime; _ufpTransform.RotateAround(_center, _ufpTransform.up, _destRoundData); //_destUfpTransform.position = _ufpTransform.position; //_destUfpTransform.rotation = _ufpTransform.rotation; //_destUfpTransform.RotateAround(_center, _ufpTransform.up, _destRoundData); Debug.Log("----------------->_characterMotion:::" + _destRoundData + ",_destRoundData::" + _destRoundData.AsFloat() + ",childObj.forward::" + _childObj.forward + ",childObj.forward.toVector3::" + _childObj.forward.ToVector()); } else if (MotionMode == E_MotionMode.MM_ON_LINE)//直走模式:按照其forward的方向行走 { _destVector = _childObj.forward * MoveSpeed * FrameSyncManager.DeltaTime; _ufpTransform.Translate(_destVector, Space.World); //_destUfpTransform.position = _ufpTransform.position; //_destUfpTransform.rotation = _ufpTransform.rotation; //_destUfpTransform.Translate(_destVector, Space.World); } // if (Physics.Raycast(RayPointObj.position + (_destUfpTransform.position - _ufpTransform.position).ToVector(), -transform.up, GroundRayLength)) if (Physics.Raycast(RayPointObj.position + (_ufpTransform.position.ToVector() - transform.position), -transform.up, GroundRayLength)) { _bHasSpeed = false; } else { _bHasSpeed = true; RenderEndUpdatePosition(); } _verCurSpeed = MoveSpeed / 2; }
public void Update() { if (_destVector != FPVector.zero) { float disData = Vector3.Distance(transform.position, _ufpTransform.position.ToVector()); FP fixDistan = MoveSpeed * Time.deltaTime; if (disData <= fixDistan) { UpdatePosition(_ufpTransform.position); UpdateRotation(_ufpTransform.rotation); // Debug.Log("======>arrived_____arrived_____arrived_____arrived_____arrived:::" + _ufpTransform.position); } else { transform.position += (_childObj.forward * fixDistan).ToVector(); // Debug.Log("======>_ufpTransform.position_OnLine:::"+ _ufpTransform.position); } } if (_destRoundData != 0) { FP angleData = _destRoundData - _roundData; FP _tempData = RotationSpeed * MoveSpeed * Time.deltaTime; //Debug.Log("======>angleData:::" + angleData.AsFloat() // + ",_tempData::" + _tempData.AsFloat() + ",_destRoundData::"+ _destRoundData); if (FPMath.Abs(angleData) <= FPMath.Abs(_tempData)) { UpdatePosition(_ufpTransform.position); UpdateRotation(_ufpTransform.rotation); _roundData = _destRoundData; //Debug.Log("======>arrived_____round_____arrived_____round_____arrived:::" + _ufpTransform.position // + ",childObj.forward::" + _childObj.forward + ",childObj.forward.toVector3::" + _childObj.forward.ToVector()); } else { transform.RotateAround(_center.ToVector(), transform.up, _tempData.AsFloat()); _roundData += _tempData; Debug.Log("======>_ufpTransform.round_round_OnLine:::" + _ufpTransform.position + ",_ufpTransform.position.toVector3::" + _ufpTransform.position // + ", ufpTransform.rotation:::" + _ufpTransform.rotation // + ",_ufpTransform.rotation.toVector()::" + _ufpTransform.rotation.ToQuaternion() + ",childObj.forward::" + _childObj.forward + ",childObj.forward.toVector3::" + _childObj.forward.ToVector()); } } RaycastHit hitInfo; bool bExitBarrier = (RayBarrierInfo(_childObj.forward.ToVector(), out hitInfo)); RaycastHit _groundHit; RaycastHit _hitInfo; if (bJumpFlag) //处理跳跃 { if (!bJumpArtFlag) //表示第一次开始跳跃 { if (!RayGroundInfo(out _groundHit, true)) { bJumpArtFlag = true; } } else { if (RayGroundInfo(out _groundHit, true)) { bJumpFlag = false; bJumpArtFlag = false; return; } } _jumpSpeed = _jumpSpeed - g * FrameSyncManager.DeltaTime; //垂直上的初速度随时间的改变 _ufpTransform.Translate(FPVector.up * _jumpSpeed * FrameSyncManager.DeltaTime, Space.World); //垂直上的运动 UpdateRotation(_ufpTransform.rotation); UpdatePosition(_ufpTransform.position); //transform.Translate(Vector3.up * _jumpSpeed * FrameSyncManager.DeltaTime, Space.World);//垂直上的运动 //if (_jumpSpeed < 0 && RayGroundInfo(out _groundHit, true)) //增加防止坠落的操作 //{ // transform/*.parent*/.position = new Vector3(transform/*.parent*/.position.x, _groundHit.point.y, transform/*.parent*/.position.z); // bJumpFlag = false; // bJumpArtFlag = false; // return; //} if (bMoveFlag) { _ufpTransform.Translate(_childObj.forward * MoveSpeed * FrameSyncManager.DeltaTime, Space.World);//水平上的运动 UpdateRotation(_ufpTransform.rotation); UpdatePosition(_ufpTransform.position); // transform.Translate(_childObj.forward.ToVector() * (MoveSpeed/** _frameEntityObj.SpeedRate*/).AsFloat() * Time.fixedDeltaTime, Space.World);//水平上的运动 } return; } else if (!RayGroundInfo(out _groundHit) && !RayBarrierInfo(_childObj.forward.ToVector(), out _hitInfo) && _bHasSpeed)//空中调整角度 { //Debug.Log("_childObj.forward.ToVector():::" + _childObj.forward.ToVector() // + ",,,,data__ufpTransform.up::" + _ufpTransform.up.ToVector() + ",,,,,fff::"+ _ufpTransform.forward.ToVector()); _verCurSpeed = _verCurSpeed - g * FrameSyncManager.DeltaTime; //垂直上的初速度随时间的改变 _ufpTransform.Translate(_childObj.forward * MoveSpeed * FrameSyncManager.DeltaTime, Space.World); //水平上的运动 _ufpTransform.Translate(FPVector.up * _verCurSpeed * FrameSyncManager.DeltaTime, Space.World); //垂直上的运动 UpdateRotation(_ufpTransform.rotation); UpdatePosition(_ufpTransform.position); // transform.Translate(_childObj.forward.ToVector() * (MoveSpeed/** _frameEntityObj.SpeedRate*/).AsFloat() * Time.fixedDeltaTime, Space.World);//水平上的运动 // transform.Translate(Vector3.up * _verCurSpeed.AsFloat() * Time.fixedDeltaTime, Space.World);//垂直上的运动 FP angleForward = FPVector.Angle(_ufpTransform.up, FPVector.up); if (angleForward == 0) { return; } FPVector normal = FPVector.Cross(_ufpTransform.up, FPVector.up); // int DirctData = FPMath.Sign(FPVector.Dot(normal, _ufpTransform.up)); float DirctData = Mathf.Sign(Vector3.Dot(normal.ToVector(), _ufpTransform.up.ToVector())); //Debug.Log(" angleForward::" + angleForward.AsFloat() + ",DirctData::"+ DirctData + ",_ufpTransform.up::"+ _ufpTransform.up + " ,"+ _ufpTransform.up.ToVector() + ",FPVector.up::"+ FPVector.up.ToVector() // + ",normal::" + normal + "," + normal.ToVector() + ", FPVector.Dot(normal, _ufpTransform.up)::" + FPVector.Dot(normal, _ufpTransform.up).AsFloat()); //if (DirctData == 0) DirctData = 1; angleForward = angleForward * DirctData; // Debug.Log(" FPMath.Sign(FPVector.Dot(normal, _ufpTransform.up)::" + FPVector.Dot(new FPVector(0,0,1), new FPVector(1, 0, 0))); FPVector forwardVec3 = FPQuaternion.AngleAxis(angleForward, normal) * _ufpTransform.up; FPVector forwardForward = FPQuaternion.AngleAxis(angleForward, normal) * _ufpTransform.forward; FPQuaternion qur = FPQuaternion.LookRotation(forwardForward, forwardVec3); //Debug.Log("forwardForward:::" + forwardForward.ToVector() + ",,forwardVec3::" + forwardVec3.ToVector() // + ",angleForward::"+ angleForward.AsFloat() // + ",_ufpTransform.up::" + _ufpTransform.up.ToVector() + ", _ufpTransform.forward::" + _ufpTransform.forward.ToVector() // + ",normal::" + normal.ToVector()); UpdateRotation(FPQuaternion.Slerp(_ufpTransform.rotation, qur, 0.1f)); //_ufpTransform.SetRotation(); //transform.rotation = FPQuaternion.Slerp(_ufpTransform.rotation, qur, 0.1f); //将玩家处于空中的状态事件发射出去 TriggerEvent(DefineEventId.PlayerInAirEvent); } else { FP angle = FPVector.Angle(FPVector.up, _ufpTransform.ChangeVec3ToTSVec(_groundHit.normal)); if (angle > SlopeAngle) { if (!bMoveFlag) { _ufpTransform.Translate(-1 * (FPVector.up) * g * FrameSyncManager.DeltaTime, Space.World); UpdateRotation(_ufpTransform.rotation); UpdatePosition(_ufpTransform.position); } } else { // transform.position = new Vector3(transform/*.parent*/.position.x, _groundHit.point.y, transform/*.parent*/.position.z); UpdateRotation(_ufpTransform.rotation); UpdatePosition(new FPVector(_ufpTransform.position.x, (FP)(_groundHit.point.y), _ufpTransform.position.z)); } } }
public void Update() { if (Target == null) { return; } // ChangeDirectionByDragging(); //if (_dragging && _directorToLeft && !_bTouchMouse) //左遥感 //{ // _rockerControl = ComMoveController.RockerControl.LeftControl; // _ufpTransform.RotateAround(_playerTransformObj.position, FPVector.up, FrameSyncManager.DeltaTime * RockerSpeed); // _ufpTransform.UpdateAllData(); // Debug.Log("_ufpTransform:::" + _ufpTransform.forward.ToVector()); // //transform.RotateAround(_childObj.position, Vector3.up, Time.deltaTime * RockerSpeed); // return; //} //else if (_dragging && _directorToRight && !_bTouchMouse) //右遥感 //{ // _rockerControl = ComMoveController.RockerControl.RightControl; // _ufpTransform.RotateAround(_playerTransformObj.position, FPVector.up, FrameSyncManager.DeltaTime * -RockerSpeed); // _ufpTransform.UpdateAllData(); // // transform.RotateAround(_childObj.position, Vector3.up, Time.deltaTime * -RockerSpeed); // return; //} //Debug.Log("_currentAngle:::" + _currentAngle + ",_childObj.eulerAngles.y::" + _childObj.eulerAngles.y // + ",transform.rotation::"+ transform.rotation); RaycastHit GroundHitInfo; RaycastHit BarrieHitrInfo; bool bGroundInfoFlag = _motionObj.RayGroundInfo(out GroundHitInfo); bool bBarrierInfoFlag = _motionObj.RayBarrierInfo(_childTransformObj.forward.ToVector(), out BarrieHitrInfo); FPVector fpGroundInfoNormal = FPVector.zero; if (bGroundInfoFlag) { fpGroundInfoNormal = GroundHitInfo.normal.ToFPVector(); } if (!_dragging && !_bTouchMouse && _bBeforeDragging) { _rockerControl = ComMoveController.RockerControl.None; return; } else if (!_dragging) { if (_bBeforeDragging) //在这里设置值,为了保证childObj的forward朝向跟摄像机的一致,防止先后差值 { _bBeforeDragging = false; return; } if (!bGroundInfoFlag) //在空中 { _rockerControl = ComMoveController.RockerControl.AirControl; if (_motionObj.bJumpFlag && false) //这是处理不跟随着对象跳跃的逻辑部分 { Distance = FPMath.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseScrollWheelSensitivity), DistanceMin, DistanceMax); FPVector DataVec3 = new FPVector(_playerTransformObj.position.x, transform.position.y - Height, _playerTransformObj.position.z); _ufpTransform.position = (_ufpTransform.rotation * new FPVector(.0f, Height, -Distance)) + DataVec3; _ufpTransform.UpdateAllData(); return; } } else if (!bBarrierInfoFlag || !_motionObj.JudgetGroundSlope(BarrieHitrInfo.normal)) //有地面接触但前方没有障碍物 { if (!_motionObj.JudgetGroundSlope(GroundHitInfo.normal)) { _rockerControl = ComMoveController.RockerControl.OtherControl; _climbOtherWall = false; } else { if (_climbOtherWall && _beforeWallNormalVec3 != fpGroundInfoNormal) //表示从一面墙跨到另外一面墙 { _beforeGroundNormalVec3 = _beforeWallNormalVec3; _beforeWallNormalVec3 = fpGroundInfoNormal; } _rockerControl = ComMoveController.RockerControl.ClimbControl; } } else //有地面接触且前方有障碍物 { _rockerControl = ComMoveController.RockerControl.None; _beforeControl = ComMoveController.RockerControl.OtherControl; if (!_motionObj.JudgetGroundSlope(GroundHitInfo.normal)) //从地面跨到墙上的情况 { _beforeGroundNormalVec3 = fpGroundInfoNormal; } else //从一面墙跨到另外一面墙的情况 { _climbOtherWall = true; _beforeWallNormalVec3 = fpGroundInfoNormal; //设置这个变量的原因是:有可能检测到障碍物,但是玩家并没有跨越过去 } } } if (_rockerControl == RockerControl.AirControl) { Distance = FPMath.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseScrollWheelSensitivity), DistanceMin, DistanceMax); _ufpTransform.position = (_ufpTransform.rotation * new FPVector(.0f, Height, -Distance)) + _playerTransformObj.position; _ufpTransform.UpdateAllData(); } else if (_rockerControl == RockerControl.OtherControl) { var quaternion = FPQuaternion.AngleAxis((_currentAngle) + _childTransformObj.transform.eulerAngles.y, FPVector.up); Distance = FPMath.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseScrollWheelSensitivity), DistanceMin, DistanceMax); _ufpTransform.SetRotation(quaternion); // transform.rotation = quaternion; FPVector data = (_ufpTransform.rotation * new FPVector(.0f, Height, -Distance)); _ufpTransform.position = (_ufpTransform.rotation * new FPVector(.0f, Height, -Distance)) + _playerTransformObj.position; _ufpTransform.UpdatePosition(); //Debug.Log("data::"+ data.ToVector()+ ", _ufpTransform.position::::" + _ufpTransform.position.ToVector() // + ",transform::" + transform.position + "::_playerTransformObj.position:" + _playerTransformObj.position.ToVector() // + ",,name::" + _playerTransformObj.gameObject.name); _rotation = _ufpTransform.rotation; _ufpTransform.UpdateForward(); } else if (_rockerControl == RockerControl.ClimbControl) { Distance = FPMath.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseScrollWheelSensitivity), DistanceMin, DistanceMax); var quaternion = FPQuaternion.AngleAxis((0) + transform.eulerAngles.y, FPVector.up); _ufpTransform.position = (_ufpTransform.rotation * new FPVector(.0f, Height, -Distance)) + _playerTransformObj.position; _ufpTransform.UpdateAllData(); FPVector climbForward = _ufpTransform.forward; if (_beforeControl == ComMoveController.RockerControl.OtherControl && _beforeGroundNormalVec3 != FPVector.zero) { FP tempAngle = FPVector.Angle(_beforeGroundNormalVec3, fpGroundInfoNormal); FPVector normal = FPVector.Cross(_beforeGroundNormalVec3, fpGroundInfoNormal); //叉乘求出法线向量 // num *= Mathf.Sign(Vector3.Dot(normal, info.transform.up)); //求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向 climbForward = FPQuaternion.AngleAxis((90 - tempAngle), normal) * fpGroundInfoNormal; climbForward = -1 * climbForward; _finishWallNormalVec3 = climbForward; _beforeControl = ComMoveController.RockerControl.ClimbControl; } FP forwardAngle = FPVector.Angle(_finishWallNormalVec3, _ufpTransform.forward); if (forwardAngle != 0 && false) //处理摄像机角度偏转 { //1)调整摄像机的旋转角度 float direcFlag = -1; FPVector normalVec3 = FPVector.Cross(_finishWallNormalVec3, _ufpTransform.forward); //叉乘求出法线向量 direcFlag *= FPMath.Sign(Vector3.Dot(normalVec3.ToVector(), _ufpTransform.up.ToVector())); //求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向 forwardAngle *= direcFlag; FPVector beforeForward = _ufpTransform.forward; FPVector forward = FPQuaternion.AngleAxis(forwardAngle, _ufpTransform.up) * _ufpTransform.forward; // Debug.Log("_ufpTransform.forward::" + _ufpTransform.forward.ToVector() + ",forward::" + forward.ToVector() // + "forwardAngle:::" + forwardAngle.AsFloat() + ",forward1111::" + forward); float quaternionSpeed = 0.003f; if (!_bTouchMouse) { quaternionSpeed = 0.03f; } if (beforeForward != forward) { Debug.Log("LookRotation(forward):::" + FPQuaternion.LookRotation(forward) + ",_rotation::" + _rotation + ",unity::" + Quaternion.LookRotation(forward.ToVector())); _rotation = FPQuaternion.Slerp(_rotation, FPQuaternion.LookRotation(forward), quaternionSpeed); _ufpTransform.SetRotation(_rotation); } // Debug.Log(",forward::"+ forward.ToVector() + ",_ufpTransform.forward::" + _ufpTransform.forward.ToVector()); //2)调整人物的旋转角度 if (!_climbOtherWall) // 这是从地面爬到墙得处理,如果是从一面墙爬到另外一面墙,镜头不做转换 { Debug.Log("beforeForward:::" + beforeForward.ToVector() + ",_ufpTransform.forward::" + _ufpTransform.forward.ToVector()); _offsetAngle = FPVector.Angle(beforeForward, _ufpTransform.forward) * direcFlag; _avatarObj.ChangeAvaterForward(_offsetAngle); } } } Debug.DrawLine(_ufpTransform.position.ToVector(), _playerTransformObj.transform.position, Color.red); if (_rockerControl == RockerControl.OtherControl || _rockerControl == RockerControl.ClimbControl) { //看是否有障碍物 FPVector directionTarget = (_ufpTransform.position - _ufpTransform.ChangeVec3ToTSVec(_rayPointObj.position)).normalized; FP distance = FPVector.Distance(_ufpTransform.position, _ufpTransform.ChangeVec3ToTSVec(_rayPointObj.position)); if (distance > Distance) { _ufpTransform.Translate(directionTarget * (distance - Distance)); _ufpTransform.UpdateRotationAndPosition(); } // Debug.DrawRay(_rayPointObj.position, directionTarget * Distance, Color.black); int layerMask = LayerMask.GetMask(Layers.Render); RaycastHit info; if (Physics.Raycast(_rayPointObj.position, directionTarget.ToVector(), out info, Distance.AsFloat(), layerMask)) //如果 { // Debug.Log("info.name::" + info.transform.name); if (info.transform.name != transform.name /*&& info.transform.tag != Tags.Ground*/) { _ufpTransform.SetPosition(_ufpTransform.ChangeVec3ToTSVec(info.point)); //transform.position = info.point; } if (_rockerControl == RockerControl.OtherControl) { _beforeControl = RockerControl.OtherControl; } } } }