示例#1
0
    private void PlayerMovement()
    {
        float horiInput = Input.GetAxis(horizontalInputName);
        float vertInput = Input.GetAxis(verticalInputName);


        //Blue Arrow is always pointing Forward relative to the game object
        //Red Arrow is always pointing Right relative to the game object.
        //Manipulate these, and we got movement
        Vector3 forwardMovement = transform.forward * vertInput;
        Vector3 rightMovement   = transform.right * horiInput;

        charController.SimpleMove(Vector3.ClampMagnitude(forwardMovement + rightMovement, 1.0f) * movementSpeed);
        //ClampMagnitude helps clamp forward direction and right direct never go beyond the value of 1
        //Helps prevent diagonal movement to go faster than the other axis


        //Complicated bits of code that checks if we are on a slope or not
        //Its a bit weird, but the way it works is that we cast a raycast with OnSlope
        //If the raycasts says we are on a slope, ,we force it down onto it.
        if ((vertInput != 0 || horiInput != 0) && OnSlope())
        {
            charController.Move(Vector3.down * charController.height / 2 * slopeForce * Time.deltaTime);
        }
        JumpInput();
    }
示例#2
0
    private Vector3 FlowFieldSteering()
    {
        Vector3 predictedPosition = transform.position + currentVelocity * flowFieldPredictionTime;
        Node    predictedNode     = map.WorldToNode(predictedPosition);
        Vector3 desiredVelocity;

        if (predictedNode == flowField.targetNode) // this is so unit doesn't just stop at the edge of target node
        {
            // if evaluated node is targetNode, move towards targetPosition located inside targetNode (gradually slow down)
            desiredVelocity = targetPosition - predictedPosition;
        }
        else
        {
            // if evaluated node is NOT targetNode, follow flowDirection (max speed)
            desiredVelocity = predictedNode.flowDirection;
        }

        desiredVelocity *= maxVelocity;
        desiredVelocity  = Vector3.ClampMagnitude(desiredVelocity, maxVelocity);
        // calculate steering
        Vector3 steering = desiredVelocity - currentVelocity; // change in velocity

        if (steering != Vector3.zero)
        {
            steeringCount++;
            return(new Vector3(steering.x, 0, steering.z));
        }

        return(Vector3.zero);
    }
示例#3
0
    void Update()
    {
        playerInput.x = Input.GetAxis("Horizontal");
        playerInput.y = Input.GetAxis("Vertical");
        playerInput.z = Swimming ? Input.GetAxis("UpDown") : 0f;
        playerInput   = Vector3.ClampMagnitude(playerInput, 1f);

        if (playerInputSpace)
        {
            rightAxis   = ProjectOnContactPlane(playerInputSpace.right, upAxis);
            forwardAxis = ProjectOnContactPlane(playerInputSpace.forward, upAxis);
        }
        else
        {
            rightAxis   = ProjectOnContactPlane(Vector3.right, upAxis);
            forwardAxis = ProjectOnContactPlane(Vector3.forward, upAxis);
        }

        if (Swimming)
        {
            desiresClimbing = false;
        }
        else
        {
            desiredJump    |= Input.GetButtonDown("Jump");
            desiresClimbing = Input.GetButton("Climb");
        }

        meshRenderer.material = Climbing ? climbingMaterial :
                                Swimming ? swimmingMaterial : normalMaterial;
        // meshRenderer.material.color = Color.white * submergence;
    }
示例#4
0
    private Vector3 TotalSteering()
    {
        steeringCount = 0;
        Vector3 steering = Vector3.zero;             // change in velocity = acceleration * time

        steering += QueueSteering() * weights.queue; // this also updates isQueueing
        steering += SeparationSteering() * weights.separation;

        if (!isQueueing)
        {
            steering += ArrivalSteering() * weights.arrival; // this also updates isArriving
            if (!isArriving)                                 // is moving according to flow field
            {
                steering += FlowFieldSteering() * weights.flowField;
            }
        }

        if (steeringCount > 0)
        {
            steering /= steeringCount; // find average length
            steering  = Vector3.ClampMagnitude(steering, maxSteering);
            return(steering);
        }
        return(Vector3.zero);
    }
示例#5
0
        /** Calculate the AI's next position (one frame in the future).
         * \param direction The tangent of the segment the AI is currently traversing. Not normalized.
         */
        protected virtual Vector3 CalculateNextPosition(out Vector3 direction, float deltaTime)
        {
            if (!interpolator.valid)
            {
                direction = Vector3.zero;
                return(simulatedPosition);
            }

            interpolator.distance += deltaTime * speed;

            if (interpolator.remainingDistance < 0.0001f && !reachedEndOfPath)
            {
                reachedEndOfPath = true;
                OnTargetReached();
            }

            direction = interpolator.tangent;
            pathSwitchInterpolationTime += deltaTime;
            var alpha = switchPathInterpolationSpeed * pathSwitchInterpolationTime;

            if (interpolatePathSwitches && alpha < 1f)
            {
                // Find the approximate position we would be at if we
                // would have continued to follow the previous path
                Vector3 positionAlongPreviousPath = previousMovementOrigin + Vector3.ClampMagnitude(previousMovementDirection, speed * pathSwitchInterpolationTime);

                // Interpolate between the position on the current path and the position
                // we would have had if we would have continued along the previous path.
                return(Vector3.Lerp(positionAlongPreviousPath, interpolator.position, alpha));
            }
            else
            {
                return(interpolator.position);
            }
        }
示例#6
0
    private void Walk()
    {
        //Check for walking
        moveVector = new Vector3(rewiredPlayer.GetAxis2DRaw("Horizontal", "Vertical").x, rewiredPlayer.GetAxis2DRaw("Horizontal", "Vertical").y, 0);

        transform.position += Vector3.ClampMagnitude(moveVector, 1) * Speed * Time.deltaTime;
    }
示例#7
0
    public override void Act(GameObject player, GameObject npc)
    {
        steering  = wanderForce;
        steering  = Vector3.ClampMagnitude(steering, MaxForce);
        steering /= npc.GetComponent <Rigidbody>().mass;

        velocity = Vector3.ClampMagnitude(velocity + steering, MaxSpeed);

        npc.transform.LookAt(npc.transform.position + velocity);
        npc.transform.position += velocity * Time.fixedDeltaTime;
    }
示例#8
0
    // MOVEMENT BEHAVIORS (STEERING)

    private void UpdatePosition()
    {
        targetPosition = flowField.targetPosition; // delete this later
        Vector3 steering    = TotalSteering();
        Vector3 newVelocity = currentVelocity + steering;

        newVelocity = Vector3.ClampMagnitude(newVelocity, maxVelocity);

        // move unit according to new velocity
        Vector3 positionChange = (currentVelocity + newVelocity) * (0.5f * Time.deltaTime);

        selfRigidbody.MovePosition(transform.position + positionChange);
        currentVelocity = newVelocity;
    }
示例#9
0
    private Vector3 SeparationSteering()
    {
        if (numUnitsDetected > 1)
        {
            Vector3 desiredVelocity = Vector3.zero;
            int     numUnitsValid   = 0;
            for (int i = 0; i < numUnitsDetected; i++)
            {
                Collider unitCollider = unitsDetected[i];
                // if unitCollider does not belong to the unit calling the function
                if (unitCollider != selfCollider)
                {
                    Vector3 awayFromUnit = transform.position - unitCollider.transform.position;

                    float sqrMagnitude = awayFromUnit.sqrMagnitude;
                    // if awayFromUnit is not a zero vector AND the unit is within avoidanceRadius
                    if (sqrMagnitude != 0 && sqrMagnitude <= separationRadius * separationRadius)// && CheckWithinFOV(unitCollider.transform.position, 90))
                    {
                        numUnitsValid++;
                        // scale so that closer to this unit => higher desiredVelocity magnitude
                        awayFromUnit    /= sqrMagnitude; // this normalizes and scales magnitude to be between 0 and 1
                        desiredVelocity += awayFromUnit;
                    }
                }
            }

            if (numUnitsValid > 0)
            {
                desiredVelocity /= numUnitsValid; // find average magnitude (desiredVelocity is now between 0 and 1)
                desiredVelocity *= maxVelocity;   // scale to maxVelocity (desiredVelocity should now be between 0 and maxVelocity)
                desiredVelocity  = Vector3.ClampMagnitude(desiredVelocity, maxVelocity);
                Vector3 steering = desiredVelocity - currentVelocity;
                if (steering != Vector3.zero)
                {
                    steeringCount++;
                    return(new Vector3(steering.x, 0, steering.z));
                }

                return(Vector3.zero);
            }
        }

        // if this statement is reached, it means there were no units within separation radius
        return(Vector3.zero);
    }
    private void FixedUpdate()
    {
        if (freezerSkillPicked)
        {
            return;
        }

        if (isMyChar)
        {
            float horizontalDirection = Input.GetAxis("Horizontal");
            float verticalDirection   = Input.GetAxis("Vertical");
            rb.velocity = Vector3.ClampMagnitude(horizontalDirection * Vector3.right + verticalDirection
                                                 * Vector3.forward, 1f) * characterSpeed * Time.fixedDeltaTime;
        }
        // Freeze character position on Y axis, rigidbody constrains not work well.
        transform.position = Vector3.up * posY +
                             Vector3.forward * transform.position.z +
                             Vector3.right * transform.position.x;
    }
示例#11
0
文件: Shoot.cs 项目: MikeMG-PL/Hailon
    void DragRelease()
    {
        dragging = false;
        lineRenderer.positionCount = 0;

        dragReleasePosition   = camera.ScreenToWorldPoint(touch.position);
        dragReleasePosition.z = 0;

        Vector3 force        = dragStartPosition - dragReleasePosition;
        Vector3 clampedForce = Vector3.ClampMagnitude(force, maxDrag) * power;

        if (newBall != null)
        {
            ballRigidbody.AddForce(clampedForce, ForceMode2D.Impulse);
            ballRigidbody.constraints = RigidbodyConstraints2D.None;
            ballRigidbody.GetComponent <CircleCollider2D>().isTrigger = false;
            newBall.transform.SetParent(null);
            newBall.GetComponent <CapsuleCollider2D>().enabled = false;
            newBall = null;
        }

        StartCoroutine(SetUpNewBall(0.5f));
    }
    // Returns the horizontal velocity based on player input
    Vector3 CalculateVelocityHorizontal()
    {
        // Take in player input and current velocity
        Vector3 currentVelocity = new Vector3(velocityHorizontal.x, 0, velocityHorizontal.z);
        Vector3 newVelocity     = currentVelocity;
        Vector3 inputDirection  = (transform.forward * Input.GetAxisRaw("Vertical") +
                                   transform.right * Input.GetAxisRaw("Horizontal")).normalized;

        // Apply friction
        if (isGrounded)
        {
            newVelocity = VelocityBraked(newVelocity);
        }

        // Apply friction and find direction of acceleration
        Vector3 acceleration = inputDirection * maxAcceleration;

        acceleration = Vector3.ClampMagnitude(acceleration, GetMaxSpeed());
        Vector3 accelDir = acceleration.normalized;

        // Calculate veer and the speed added
        float veer     = currentVelocity.x * accelDir.x + currentVelocity.z * accelDir.z;
        float addSpeed = (isGrounded ? acceleration : Vector3.ClampMagnitude(acceleration, airSpeedCap))
                         .magnitude - veer;

        // Add new speed to velocity
        if (addSpeed > 0f)
        {
            float accelMult = isGrounded ? groundAccelMult : airAccelMult;
            acceleration *= accelMult * Time.deltaTime;
            acceleration  = Vector3.ClampMagnitude(acceleration, addSpeed);
            newVelocity  += acceleration;
        }

        // Return the final velocity
        return(newVelocity);
    }
示例#13
0
 private void FixedUpdate()
 {
     _rb.velocity     = Vector3.ClampMagnitude(_rb.velocity, _maxSpeed);
     _directionVector = (RbTransform.right * _moveDirection.x) + (RbTransform.forward * _moveDirection.z);
     _rb.MovePosition(RbTransform.position + _directionVector * _walk * Time.deltaTime);
 }