예제 #1
0
        private void Update()
        {
            if (ttl == -1)
            {
                return;
            }

            localTime += Time.deltaTime;
            speed     += acceleration * Time.deltaTime;


//            angleSpeed += rotAccel * dt;
            Quaternion rot      = transform.rotation;
            Quaternion rotForce = MUtil.toAngleAxis(angleSpeed.length() * Time.deltaTime, angleSpeed.normalized);

            rot = rotForce * rot;

            transform.SetPositionAndRotation(transform.position + speed * Time.deltaTime, rot);

//            scale += scaleSpeed * Time.deltaTime;
//            if (scale <= 0) Destroy(gameObject);

//            transform.localScale = new Vector3(scale, scale, scale);
            if (localTime >= ttl)
            {
                Destroy(gameObject);
            }
        }
예제 #2
0
        public override void fixedTick(float dt)
        {
            base.fixedTick(dt);

            if (target == null)
            {
                if (transform.parent == null)
                {
                    targetPos = initialLocalPos;
                }
                else
                {
                    targetPos = transform.parent.TransformPoint(initialLocalPos);
                }
            }

            if (!Application.isPlaying)
            {
                return;
            }
            if (rotate)
            {
                Quaternion curRotAbs = transform.rotation;
                if (rb == null)
                {
                    Vector3 rotAccel = Stepper5.torqueFromQuaternions(rotationMotor, curRotAbs, targetRot, angleSpeed, 1);
                    angleSpeed += rotAccel * dt;
                    Quaternion rotForce = MUtil.toAngleAxis(angleSpeed.length() * Time.deltaTime, angleSpeed.normalized);
                    transform.rotation = rotForce * curRotAbs;
                }
                else
                {
                    rb.AddTorque(Stepper5.torqueFromQuaternions(rotationMotor, curRotAbs, targetRot, rb.angularVelocity, 1), ForceMode.Acceleration);
                }
            }

            if (move)
            {
                Vector3 curPosAbs = transform.position;
                if (rb == null)
                {
                    Vector3 accel = posMotor.getAccel(targetPos, curPosAbs, speed, 1);
                    speed += accel * dt;
                    transform.position = curPosAbs + speed * dt;
                }
                else
                {
                    rb.AddForce(posMotor.getAccel(targetPos, curPosAbs, rb.velocity, 1), ForceMode.Acceleration);
                }
            }
        }
예제 #3
0
        public void FixedUpdate()
        {
            if (!enabled)
            {
                return;
            }
            if (bodyRigid == null)
            {
                return;
            }
            if (cam == null)
            {
                return;
            }
            bool grabInput = focusGrabber != null && focusGrabber.grab;

            if (Application.isPlaying)
            {
                calcUpByLegs();
            }

            float h           = 0;
            int   dockedCount = 0;

            for (int i = 0; i < moveen.engine.steps.Count; i++)
            {
                Step2 step = moveen.engine.steps[i];

//                if (!step.dockedState) { !!falls through on BM2 as best target can be -100500
//                    h += step.bestTargetProgressiveAbs.y; //to rise sharper
//                } else {
                dockedCount++;
                h += step.posAbs.y;
//                }
//                h += step.bestTargetProgressiveAbs.y;
            }
            float targetHeightMultiplier = jumpPreparing ? jumpPrepareHeightMul : jumpInProgress ? jumpTargetHeightMul : 1;

            h = moveen.engine.steps.Count == 0 ? height : (h / moveen.engine.steps.Count + height * targetHeightMultiplier);

            float maxSpeed = speed;

            if (Input.GetKey(KeyCode.LeftShift))
            {
                maxSpeed *= runSpeedMultiplier;
                moveen.engine.runJumpTime = runJumpTime;//it adds additional jump force on the step
            }
            else
            {
                moveen.engine.runJumpTime = 0;
            }
            moveen.engine.horizontalMotor.maxSpeed = maxSpeed;

            Vector3 add         = new Vector3();
            bool    movePressed = false;

            if (jumpStrengthCurTime < jumpPrepareTime)
            {
                if (jumpStrengthCurTime > 0)
                {
                    //set only after space was released (jump started), as while space is pressed jumpStrengthCurTime is 0
                    moveen.engine.verticalMotor.copyFrom(jumpMotor);
                    jumpInProgress = true;
                }
                jumpStrengthCurTime += Time.deltaTime;
                //revert old value when jump is ended
                if (jumpStrengthCurTime >= jumpPrepareTime)
                {
                    moveen.engine.verticalMotor.copyFrom(previousVerticalMotor);
                    jumpInProgress = false;
                }
            }
            //remember value from editor when not jumping
            if (jumpStrengthCurTime >= jumpPrepareTime)
            {
                previousVerticalMotor.copyFrom(moveen.engine.verticalMotor);
            }

            if (grabInput)
            {
                if (Input.GetKey(KeyCode.Space))
                {
                    jumpStrengthCurTime = 0;
                    jumpPreparing       = true;
                }
                else
                {
                    jumpPreparing = false;
                }
                if (Input.GetKey(KeyCode.W) || debugMoveForward)
                {
                    add        += new Vector3(1, 0, 0);
                    movePressed = true;
                }
                if (Input.GetKey(KeyCode.S))
                {
                    add        += new Vector3(-1, 0, 0);
                    movePressed = true;
                }
                if (Input.GetKey(KeyCode.A))
                {
                    add        += new Vector3(0, 0, 1);
                    movePressed = true;
                }
                if (Input.GetKey(KeyCode.D))
                {
                    add        += new Vector3(0, 0, -1);
                    movePressed = true;
                }
                add  = add.normalized();
                add *= (maxSpeed / moveen.engine.horizontalMotor.distanceToSpeed);

                //cam local
                add = MUtil.toAngleAxis(camYaw, new Vector3(0, 1, 0)).rotate(add); //cam local -> global
                add = bodyRigid.transform.rotation.conjug().rotate(add);           //global -> body local


                if (add.x > 0)
                {
                    add.x = Math.Min(add.x, maxSpeed);
                }
                if (add.x < 0)
                {
                    add.x = Math.Max(add.x, -maxSpeed * rearSpeedMultiplyer);
                }
                if (add.z > 0)
                {
                    add.z = Math.Min(add.z, maxSpeed * strafeSpeedMultiplyer);
                }
                if (add.z < 0)
                {
                    add.z = Math.Max(add.z, -maxSpeed * strafeSpeedMultiplyer);
                }


//            add = transform.rotation.rotate(add); //body local -> global
                add = bodyRigid.transform.rotation.rotate(add); //body local -> global
//            transform.position = body.transform.position + body.transform.rotation.rotate(add);
            }

            Vector3 newBestTargetPos = moveen.engine.imCenter.add(add).withSetY(h);

            if (quantCenter)
            {
                if (dockedCount == 0 || movePressed || newBestTargetPos.dist(transform.position) > quantSize)
                {
                    transform.position = newBestTargetPos;
                }
            }
            else
            {
                transform.position = newBestTargetPos;
            }

            wantedCamera = bodyRigid.position.withSetY(h);
//            wantedCamera = transform.position.limit(bodyRigid.position.withSetY(h), maxSpeed / 2).withSetY(h);//мерзко дрожит камера на больших скоростях

            if (grabInput)
            {
                float mx = Input.GetAxis("Mouse X") * 0.04f / Time.timeScale;
                float my = Input.GetAxis("Mouse Y") * -0.04f / Time.timeScale;
                camPitch = MyMath.clamp(camPitch + my, -1, 1);
                if (!debugFreezeRotation)
                {
                    camYaw = MyMath.angleNormalizeSigned(camYaw + mx); //comment to stop reacting on mouse
                }
                if (debugRotateRight)
                {
                    camYaw = MyMath.angleNormalizeSigned(camYaw + 0.3f * 0.05f); //uncomment to add constant rotation for test
                }
            }
            Quaternion yawRoll = MUtil.toAngleAxis(camYaw, new Vector3(0, 1, 0));

            Quaternion rotForCameraLook = yawRoll
                                          * MUtil.toAngleAxis((float)(Math.PI / 2), new Vector3(0, 1, 0))
                                          * MUtil.toAngleAxis(camPitch, new Vector3(1, 0, 0));
            Quaternion rotForCameraPos = yawRoll;
            Quaternion rotForTarget;


//            RaycastHit hit;
//            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//            Quaternion wantedRot = transform.rotation;
//            Vector3 hitPoint = new Vector3();
//
//            bool wasHit = false;
//            if (grabInput) {
//                wasHit = Physics.Raycast(ray.origin, ray.direction, out hit); //TODO cached
//                hitPoint = hit.point;
//                if (wasHit) {
//                    //lower target, because we are looking from the top, but the actor will be aiming from a side
//                    //  but we don't want correct height for walls and terrain
////                    if (hit.normal.y > 0.5f && hit.point.y > hit.collider.transform.position.y) {
////                        hitPoint.y = hit.collider.transform.position.y;
////                    }
//                    //Debug.DrawLine(new Vector3(-100, -100, -100), hitPoint, Color.red);
//
//                    if (moveen.engine.imCenter.dist(hitPoint) > 1) {
//                        wantedRot = MUtil.qToAxes(hitPoint - moveen.engine.imCenter, Vector3.up);
//                    }
//                } else {
//                    hitPoint = ray.origin + ray.direction * 40;
//                    wasHit = true;
//                }
//            }
//
//            yawRoll = Quaternion.FromToRotation(moveen.transform.rotation.rotate(Vector3.right), hitPoint.sub(moveen.transform.position));
//            rotForCameraPos = yawRoll;
//
//
//            //must be after this transform pos/rot change as target can be child of this
//            if (wasHit && aimTarget != null) aimTarget.position = hitPoint;



            if (inclineBodyToLegs)
            {
                rotForTarget = MUtil.qToAxesYX(Vector3.up.mix(upByLegs, inclineBodyToLegsRatio), yawRoll.rotate(new Vector3(1, 0, 0)));
//                rotForTarget = MUtil.qToAxes(yawRoll.rotate(new Vector3(1, 0, 0)), upByLegs);
            }
            else
            {
                rotForTarget = yawRoll * MUtil.toAngleAxis(camPitch * -0.5f, new Vector3(0, 0, 1));
            }


            if (grabInput)
            {
                cam.transform.rotation = rotForCameraLook;
                oldCam = oldCam.mix(wantedCamera, camApproachFactor);
                cam.transform.position = oldCam + rotForCameraPos.rotate(camPosition);
            }

//            float reactionSpeed = bodyRotReactionSpeed;
//            if (bodyRigid != null) reactionSpeed /= Math.Max(1, bodyRigid.velocity.length() * bodyRotReactionSpeedSpeed);

            if (dockedCount > 0)
            {
                //rotate target only if at least one leg is touching ground
                float      cosOfHalfAngle = (float)Math.Cos(bodyRotReactionSpeed / 2 / 180f * Math.PI); //TODO cache
                Quaternion rotDif         = rotForTarget.rotSub(transform.rotation);
                if (rotDif.w < 0)
                {
                    rotDif = rotDif.scale(-1);
                }
                if (rotDif.w < cosOfHalfAngle)
                {
                    rotDif = rotDif.normalizeWithFixedW(cosOfHalfAngle);
                }
                transform.rotation = transform.rotation * rotDif;
//                transform.rotation = rotForTarget;
            }
            else
            {
                //get rotation from the body when in air
                //  because else - body could accumulate rotation it will be looking weird after grounding
                transform.rotation = moveen.targetRot;
            }

            moveen.targetPos = transform.position;
            moveen.targetRot = transform.rotation;
        }
예제 #4
0
        public void FixedUpdate()
        {
            if (!enabled)
            {
                return;
            }
            if (bodyRigid == null)
            {
                return;
            }
            if (cam == null)
            {
                return;
            }
            bool grabInput = focusGrabber != null && focusGrabber.grab;

            if (Application.isPlaying)
            {
                calcUpByLegs();
            }

            float h           = 0;
            int   dockedCount = 0;

            for (int i = 0; i < moveen.engine.steps.Count; i++)
            {
                Step2 step = moveen.engine.steps[i];

                if (!step.dockedState)
                {
                    h += step.bestTargetProgressiveAbs.y; //to rise sharper
                }
                else
                {
                    dockedCount++;
                    h += step.posAbs.y;
                }
//                h += step.bestTargetProgressiveAbs.y;
            }
            float targetHeightMultiplier = jumpPreparing ? jumpPrepareHeightMul : jumpInProgress ? jumpTargetHeightMul : 1;

            h = moveen.engine.steps.Count == 0 ? height : (h / moveen.engine.steps.Count + height * targetHeightMultiplier);

            float maxSpeed = speed;

            if (Input.GetKey(KeyCode.LeftShift))
            {
                maxSpeed *= runSpeedMultiplier;
                moveen.engine.runJumpTime = runJumpTime;//it adds additional jump force on the step
            }
            else
            {
                moveen.engine.runJumpTime = 0;
            }
            moveen.engine.horizontalMotor.maxSpeed = maxSpeed;

            Vector3 add         = new Vector3();
            bool    movePressed = false;

            if (jumpStrengthCurTime < jumpPrepareTime)
            {
                if (jumpStrengthCurTime > 0)
                {
                    //set only after space was released (jump started), as while space is pressed jumpStrengthCurTime is 0
                    moveen.engine.verticalMotor.copyFrom(jumpMotor);
                    jumpInProgress = true;
                }
                jumpStrengthCurTime += Time.deltaTime;
                //revert old value when jump is ended
                if (jumpStrengthCurTime >= jumpPrepareTime)
                {
                    moveen.engine.verticalMotor.copyFrom(previousVerticalMotor);
                    jumpInProgress = false;
                }
            }
            //remember value from editor when not jumping
            if (jumpStrengthCurTime >= jumpPrepareTime)
            {
                previousVerticalMotor.copyFrom(moveen.engine.verticalMotor);
            }

            if (grabInput)
            {
                if (Input.GetKey(KeyCode.Space))
                {
                    jumpStrengthCurTime = 0;
                    jumpPreparing       = true;
                }
                else
                {
                    jumpPreparing = false;
                }
                if (Input.GetKey(KeyCode.W) || debugMoveForward)
                {
                    add        += new Vector3(1, 0, 0);
                    movePressed = true;
                }
                if (Input.GetKey(KeyCode.S))
                {
                    add        += new Vector3(-1, 0, 0);
                    movePressed = true;
                }
                if (Input.GetKey(KeyCode.A))
                {
                    add        += new Vector3(0, 0, 1);
                    movePressed = true;
                }
                if (Input.GetKey(KeyCode.D))
                {
                    add        += new Vector3(0, 0, -1);
                    movePressed = true;
                }
                add  = add.normalized();
                add *= (maxSpeed / moveen.engine.horizontalMotor.distanceToSpeed);

                //cam local
//                add = MUtil.toAngleAxis(camYaw, new Vector3(0, 1, 0)).rotate(add); //cam local -> global
//                add = bodyRigid.transform.rotation.conjug().rotate(add); //global -> body local


                if (add.x > 0)
                {
                    add.x = Math.Min(add.x, maxSpeed);
                }
                if (add.x < 0)
                {
                    add.x = Math.Max(add.x, -maxSpeed * rearSpeedMultiplyer);
                }
                if (add.z > 0)
                {
                    add.z = Math.Min(add.z, maxSpeed * strafeSpeedMultiplyer);
                }
                if (add.z < 0)
                {
                    add.z = Math.Max(add.z, -maxSpeed * strafeSpeedMultiplyer);
                }


//            add = transform.rotation.rotate(add); //body local -> global
                add = bodyRigid.transform.rotation.rotate(add); //body local -> global
//            transform.position = body.transform.position + body.transform.rotation.rotate(add);
            }

            Vector3 newBestTargetPos = moveen.engine.imCenter.add(add).withSetY(h);

            if (quantCenter)
            {
                if (dockedCount == 0 || movePressed || newBestTargetPos.dist(transform.position) > quantSize)
                {
                    transform.position = newBestTargetPos;
                }
            }
            else
            {
                transform.position = newBestTargetPos;
            }

            float leftRight = 0;

            if (grabInput)
            {
                float my = Input.GetAxis("Mouse Y") * -0.02f / Time.timeScale;

                leftRight = (Input.mousePosition.x / Screen.width - 0.5f) * 2;
                if (debugFreezeRotation)
                {
                    leftRight = 0;
                }
                if (debugRotateRight)
                {
                    leftRight = 0.5f;
                }

                if (leftRight > 0)
                {
                    leftRight = MyMath.smoothstep(0, 1, leftRight);
                }
                else
                {
                    leftRight = -MyMath.smoothstep(0, 1, -leftRight);
                }

                camPitch = MyMath.clamp(camPitch + my, -1, 1);
            }


//            RaycastHit hit;
//            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

//            Quaternion yawRoll = Quaternion.AngleAxis(leftRight * 10, Vector3.up) * transform.rotation;
            Quaternion yawRoll = Quaternion.AngleAxis(leftRight * 10, Vector3.up) * MUtil.qToAxesYX(Vector3.up, transform.rotation.rotate(Vector3.right));
//            Quaternion yawRoll = Quaternion.FromToRotation(moveen.engine.realBodyRot.rotate(Vector3.right), hitPoint.sub(moveen.transform.position));



            Quaternion rotForCameraPos = yawRoll * MUtil.toAngleAxis(-camPitch * 0.5f, new Vector3(0, 0, 1));
//            Quaternion rotForCameraLook = rotForCameraPos;
            Quaternion rotForCameraLook = yawRoll
                                          * MUtil.toAngleAxis((float)(Math.PI / 2), new Vector3(0, 1, 0))
                                          * MUtil.toAngleAxis(camPitch, new Vector3(1, 0, 0));



            Vector3 wantedDir = yawRoll.rotate(new Vector3(1, 0, 0));
//            if (wantedDir.scalarProduct(moveen.engine.inputWantedRot.rotate(Vector3.right)) > 0.95f) {
//                yawRoll = transform.rotation;
//                wantedDir = yawRoll.rotate(new Vector3(1, 0, 0));
//            }
            Quaternion rotForTarget;

            if (inclineBodyToLegs)
            {
                rotForTarget = MUtil.qToAxesYX(Vector3.up.mix(upByLegs, inclineBodyToLegsRatio), wantedDir);
//                rotForTarget = MUtil.qToAxes(yawRoll.rotate(new Vector3(1, 0, 0)), upByLegs);
            }
            else
            {
                rotForTarget = yawRoll * MUtil.toAngleAxis(camPitch * -0.5f, new Vector3(0, 0, 1));
            }


            if (grabInput)
            {
                Vector3 wantedCamera = moveen.engine.imCenter.withSetY(h);
                cam.transform.rotation = rotForCameraLook;
                oldCam = oldCam.mix(wantedCamera, camApproachFactor);
                cam.transform.position = oldCam + rotForCameraPos.rotate(camPosition);
            }

//            float reactionSpeed = bodyRotReactionSpeed;
//            if (bodyRigid != null) reactionSpeed /= Math.Max(1, bodyRigid.velocity.length() * bodyRotReactionSpeedSpeed);

            if (dockedCount > 0)
            {
                //rotate target only if at least one leg is touching ground
                float      cosOfHalfAngle = (float)Math.Cos(bodyRotReactionSpeed / 2 / 180f * Math.PI); //TODO cache
                Quaternion rotDif         = rotForTarget.rotSub(transform.rotation);
                if (rotDif.w < 0)
                {
                    rotDif = rotDif.scale(-1);
                }
                if (rotDif.w < cosOfHalfAngle)
                {
                    rotDif = rotDif.normalizeWithFixedW(cosOfHalfAngle);
                }
                transform.rotation = transform.rotation * rotDif;
//                transform.rotation = rotForTarget;
            }
            else
            {
                //get rotation from the body when in air
                //  because else - body could accumulate rotation it will be looking weird after grounding
                transform.rotation = moveen.targetRot;
            }

            moveen.targetPos = transform.position;
            moveen.targetRot = transform.rotation;


            //must be after this transform pos/rot change as target can be child of this
//            if (wasHit && aimTarget != null) aimTarget.position = hitPoint;
        }