예제 #1
0
    public Vector3 getSteering(Rigidbody target)
    {
        /* Calculate the distance to the target */
        Vector3 displacement = target.position - transform.position;
        float   distance     = displacement.magnitude;

        /* Get the character's speed */
        float speed = rb.velocity.magnitude;

        /* Calculate the prediction time */
        float prediction;

        if (speed <= distance / maxPrediction)
        {
            prediction = maxPrediction;
        }
        else
        {
            prediction = distance / speed;
        }

        /* Put the target together based on where we think the target will be */
        Vector3 explicitTarget = target.position + target.velocity * prediction;

        return(steeringBasics.seek(explicitTarget));
    }
예제 #2
0
    // Update is called once per frame
    void Update()
    {
        Vector3 accel = steeringBasics.seek(target.position);

        steeringBasics.Steer(accel);
        steeringBasics.LookWhereYoureGoing();
    }
예제 #3
0
    public Vector3 getSteering(Vector3 facingDir)
    {
        Vector3 acceleration = Vector3.zero;

        /* Creates the ray direction vector */
        Vector3[] rayDirs = new Vector3[3];
        rayDirs[0] = facingDir.normalized;

        float orientation = Mathf.Atan2(rb.velocity.y, rb.velocity.x);

        rayDirs[1] = orientationToVector(orientation + sideWhiskerAngle * Mathf.Deg2Rad);
        rayDirs[2] = orientationToVector(orientation - sideWhiskerAngle * Mathf.Deg2Rad);

        RaycastHit hit;

        /* If no collision do nothing */
        if (!findObstacle(rayDirs, out hit))
        {
            return(acceleration);
        }

        /* Create a target away from the wall to seek */
        Vector3 targetPostition = hit.point + hit.normal * wallAvoidDistance;

        /* If velocity and the collision normal are parallel then move the target a bit to
         * the left or right of the normal */
        Vector3 cross = Vector3.Cross(rb.velocity, hit.normal);

        if (cross.magnitude < 0.005f)
        {
            targetPostition = targetPostition + new Vector3(-hit.normal.y, hit.normal.x, hit.normal.z);
        }

        return(steeringBasics.seek(targetPostition, maxAcceleration));
    }
예제 #4
0
    public Vector3 getSteering(Rigidbody target)
    {
        /* 计算到目标的距离 */
        Vector3 displacement = target.position - transform.position;
        float   distance     = displacement.magnitude;

        /* 当前的速度 */
        float speed = rb.velocity.magnitude;

        /* 计算预测时间 */
        float prediction;

        if (speed <= distance / maxPrediction)   // 加速
        {
            prediction = maxPrediction;
        }
        else
        {
            prediction = distance / speed;      // 保持当前速度
        }

        /* 显式目标: 根据我们 thsink 的目标将目标放在一起*/
        Vector3 explicitTarget = target.position + target.velocity * prediction;

        Debug.Log("target.position= " + target.position + "  : prediction =" + target.velocity * prediction);

        return(steeringBasics.seek(explicitTarget));
    }
    public Vector2 getSteering(Vector2 facingDir)
    {
        Vector2 acceleration = Vector2.zero;

        /* Creates the ray direction vector */
        Vector2[] rayDirs = new Vector2[3];
        rayDirs[0] = facingDir.normalized;

        float orientation = Mathf.Atan2(rb.velocity.y, rb.velocity.x);

        rayDirs[1] = orientationToVector(orientation + sideWhiskerAngle * Mathf.Deg2Rad);
        rayDirs[2] = orientationToVector(orientation - sideWhiskerAngle * Mathf.Deg2Rad);

        RaycastHit hit;

        /* If no collision do nothing */
        if (!findObstacle(rayDirs, out hit))
        {
            return(acceleration);
        }

        /* Create a target away from the wall to seek */
        Vector2 targetPostition = hit.point + hit.normal * wallAvoidDistance;

        return(steeringBasics.seek(targetPostition, maxAcceleration));
    }
예제 #6
0
    public Vector3 getSteering()
    {
        //get the jitter for this time frame
        float jitter = wanderJitter * Time.deltaTime;

        //add a small random vector to the target's position
        wanderTarget += new Vector3(Random.Range(-1f, 1f) * jitter, 0f, Random.Range(-1f, 1f) * jitter);

        //make the wanderTarget fit on the wander circle again
        wanderTarget.Normalize();
        wanderTarget *= wanderRadius;

        //move the target in front of the character
        Vector3 targetPosition = transform.position + transform.forward * wanderDistance + wanderTarget;

        //Debug.DrawLine(transform.position, targetPosition);

        return(steeringBasics.seek(targetPosition));
    }
예제 #7
0
    public Vector3 GetSteering()
    {
        // 得到一帧时间的  最大位移
        float jitter = wanderJitter * Time.deltaTime;

        // 向目标的位置添加一个小的随机向量(每一帧都调整新的)
        wanderTarget += new Vector3(Random.Range(-1f, 1f) * jitter, Random.Range(-1f, 1f) * jitter, 0f);

        // 得到新的漫游圈
        wanderTarget.Normalize();
        wanderTarget *= wanderRadius;

        // 得到目标位置, 在角色的前方   right = ( 1, 0, 0 )
        Vector3 targetPosition = transform.position + transform.right * wanderDistance + wanderTarget;

        // 为了调试用
        Debug.DrawLine(transform.position, targetPosition);

        return(steeringBasics.seek(targetPosition));
    }
예제 #8
0
    public Vector3 getSteering(Rigidbody target)
    {
        // Calculate the distance to the target
        Vector3 displacement = target.position - transform.position;
        float   distance     = displacement.magnitude;

        // Get the character's speed
        float speed = rb.velocity.magnitude;

        // Calculate the prediction time
        float prediction;

        if (speed <= distance / maxPrediction)
        {
            prediction = maxPrediction;
        }
        else
        {
            prediction = distance / speed;
        }

        // Set where the AI thinks the target will be and look at it
        Vector3 explicitTarget = target.position + target.velocity * prediction;

        /*
         * if (distance < closingDistance)
         * {
         *  steeringBasics.LookAtDirection(target.position);
         * }
         * else
         * {
         *  steeringBasics.LookAtDirection(explicitTarget);
         * }
         */

        Debug.DrawLine(transform.position, explicitTarget);

        //transform.Translate(Vector3.forward * maxAcceleration * Time.deltaTime);
        return(steeringBasics.seek(explicitTarget));
    }
예제 #9
0
    public Vector3 GetSteering(Vector3 facingDir)
    {
        Vector3 acceleration = Vector3.zero;

        /* 创建射线方向向量 */
        Vector3[] rayDirs = new Vector3[3];
        // 自己前进的方向
        rayDirs[0] = facingDir.normalized;

        // 返回弧度,      对边y/临边x
        float orientation = Mathf.Atan2(rb.velocity.y, rb.velocity.x);

        // 两边的射线方向
        rayDirs[1] = orientationToVector(orientation + sideWhiskerAngle * Mathf.Deg2Rad);
        rayDirs[2] = orientationToVector(orientation - sideWhiskerAngle * Mathf.Deg2Rad);

        RaycastHit hit;

        /* 如果没有碰撞,什么也不做 */
        if (!findObstacle(rayDirs, out hit))
        {
            return(acceleration);
        }

        /* 从墙上创建一个目标来 seek (这个方向和射线方向相反)*/
        Vector3 targetPostition = hit.point + hit.normal * wallAvoidDistance;

        /* 如果速度和碰撞法线平行,则将目标向左或向右移动一点    (如果不矫正就会一直 垂直撞一个地方)*/
        Vector3 cross = Vector3.Cross(rb.velocity, hit.normal);   // 叉乘 判断两个向量是否平行

        // 点乘“·”计算得到的结果是一个标量;                  平行向量 normalized的点乘 是 -1 或者 1,   垂直是0
        // 叉乘“×”得到的结果是一个垂直于原向量构成平面的向量。  平行向量的叉乘是零向量
        if (cross.magnitude < 0.005f)
        {
            targetPostition = targetPostition + new Vector3(-hit.normal.y, hit.normal.x, hit.normal.z);
        }

        // 返回最大加速度
        return(steeringBasics.seek(targetPostition, maxAcceleration));
    }
예제 #10
0
    public Vector3 collisionAvoid(Vector3 target)
    {
        Vector3 dvelocity = steeringBasics.seek(target);
        Vector3 ahead     = rb.position + Vector3.Normalize(dvelocity) * maxLookAhead; // calculate the look ahead vector
        Vector3 steeravoid;
        Ray     sweep = new Ray(rb.transform.position, target);

        if (LineIntersects(ahead) == true)
        {
            Physics.Raycast(sweep, out hit, maxLookAhead);
            target    += hit.normal * maxAvoidForce;
            steeravoid = target;
            Debug.Log(steeravoid);
            return(steeravoid);
        }
        else
        {
            steeravoid = Vector3.zero;
        }

        return(steeravoid);
    }
예제 #11
0
    public Vector2 getSteering()
    {
        float characterOrientation = transform.rotation.eulerAngles.z * Mathf.Deg2Rad;

        /* Update the wander orientation */
        wanderOrientation += randomBinomial() * wanderRate;

        /* Calculate the combined target orientation */
        float targetOrientation = wanderOrientation + characterOrientation;

        /* Calculate the center of the wander circle */
        Vector2 targetPosition = (Vector2)transform.position + (orientationToVector(characterOrientation) * wanderOffset);

        //debugRing.transform.position = targetPosition;

        /* Calculate the target position */
        targetPosition = targetPosition + (orientationToVector(targetOrientation) * wanderRadius);

        //Debug.DrawLine (transform.position, targetPosition);

        return(steeringBasics.seek(targetPosition));
    }
예제 #12
0
    public Vector3 getSteering()
    {
        // 当前角色的方向(弧度)
        float characterOrientation = transform.rotation.eulerAngles.z * Mathf.Deg2Rad;

        // 随机一个漫游方向(弧度)
        wanderOrientation += randomBinomial() * wanderRate;

        // 目标方向结合(弧度), 就是在角色方向上的一个偏移
        float targetOrientation = wanderOrientation + characterOrientation;

        // 角色的前方(偏移量)
        Vector3 targetPosition = transform.position + (orientationToVector(characterOrientation) * wanderOffset);

        // 得到目标位置 向量 c = a + b 就是对角线的方向
        targetPosition = targetPosition + (orientationToVector(targetOrientation) * wanderRadius);

        Debug.DrawLine(transform.position, targetPosition);

        // 得到 最大加速度
        return(steeringBasics.seek(targetPosition));
    }
예제 #13
0
파일: Wander1.cs 프로젝트: frms/game-jams
    public Vector3 getSteering()
    {
        float characterOrientation = rb.rotationInRadians;

        /* Update the wander orientation */
        wanderOrientation += randomBinomial() * wanderRate;

        /* Calculate the combined target orientation */
        float targetOrientation = wanderOrientation + characterOrientation;

        /* Calculate the center of the wander circle */
        Vector3 targetPosition = transform.position + (SteeringBasics.orientationToVector(characterOrientation, rb.is3D) * wanderOffset);

        //debugRing.transform.position = targetPosition;

        /* Calculate the target position */
        targetPosition = targetPosition + (SteeringBasics.orientationToVector(targetOrientation, rb.is3D) * wanderRadius);

        //Debug.DrawLine (transform.position, targetPosition);

        return(steeringBasics.seek(targetPosition));
    }
예제 #14
0
 public Vector3 getSteering(Vector3 targetPosition)
 {
     //Vector3 targetAvoid = collisionAvoidance.collisionAvoid(targetPosition);
     return(steeringBasics.seek(targetPosition));
 }