美文网首页
操纵行为

操纵行为

作者: 代码记录器 | 来源:发表于2018-07-31 16:37 被阅读0次

可移动的AI角色基类

public class Vehicle : MonoBehaviour {
    //行为列表
    private Steering[] steerings;
    public float maxSpeed = 10.0f;
    public float maxForce = 100.0f;
    protected float sqrMaxSpeed;
    public float mass = 1;
    public Vector3 velocity;
    //转向速度
    public float damping = 0.9f;
    //计算间隔
    public float computeInterval = 0.2f;
    //是否在二维平面上
    public bool isPlanar = true;
    private Vector3 steeringForce;
    protected Vector3 acceleration;
    private float timer;

    protected void Start() {
        steeringForce = new Vector3 (0, 0, 0);
        sqrMaxSpeed = maxSpeed * maxSpeed;
        timer = 0;
        steerings = GetComponents<Steering> ();
    }

    void Update() {
        timer += Time.deltaTime;
        steeringForce = new Vector3 (0, 0, 0);
        if (timer > computeInterval) {
            foreach (Steering s in steerings) {
                if (s.enabled)
                    steeringForce += s.Force () * s.weight;
            }   
            steeringForce = Vector3.ClampMagnitude (steeringForce, maxForce);
            acceleration = steeringForce / mass;
            timer = 0;
        }
    }

}

AI角色移动控制类

public class AILocomotion : Vehicle {

    private CharacterController controller;
    private Rigidbody theRigidbody;
    public Vector3 moveDistance;

    void Start() {
        controller = GetComponent<CharacterController> ();
        theRigidbody = GetComponent<Rigidbody> ();
        moveDistance = new Vector3 (0, 0, 0);
        base.Start ();
    }

    void FixedUpdate() {
        velocity += acceleration * Time.fixedDeltaTime;
        if (velocity.sqrMagnitude > sqrMaxSpeed)
            velocity = velocity.normalized * maxSpeed;
        moveDistance = velocity * Time.fixedDeltaTime;
        if (isPlanar) {
            velocity.y = 0;
            moveDistance.y = 0;
        }
        if (controller != null)
            controller.SimpleMove (velocity);
        else if (theRigidbody == null || theRigidbody.isKinematic)
            transform.position += moveDistance;
        else
            theRigidbody.MovePosition (theRigidbody.position + moveDistance);
        if (velocity.sqrMagnitude > 0.00001f) {
            Vector3 newForward = Vector3.Slerp (transform.forward, velocity, damping * Time.deltaTime);
            if (isPlanar)
                newForward.y = 0;
            transform.forward = newForward;
        }
        //TODO 播放行走动画
    }
}

操控行为的基类 (抽象类)

public abstract class Steering : MonoBehaviour {

    public float weight = 1.0f;

    void Start() {
    
    }

    void Update() {
    
    }

    public virtual Vector3 Force() {
        return new Vector3 (0, 0, 0);
    }

}

行为1: 靠近

public class SteeringForSeek : Steering {

    public GameObject target;
    //预期速度
    private Vector3 desiredVelocity;
    private Vehicle m_vehicle;
    private float maxSpeed;
    private bool isPlanar;

    void Start() {
        m_vehicle = GetComponent<Vehicle> ();
        maxSpeed = m_vehicle.maxSpeed;
        isPlanar = m_vehicle.isPlanar;
    }

    public override Vector3 Force ()
    {   
        desiredVelocity = (target.transform.position - transform.position).normalized * maxSpeed;
        if (isPlanar)
            desiredVelocity.y = 0;
        return (desiredVelocity - m_vehicle.velocity);
    }
}

行为2:离开

public class SteeringForFlee : Steering {

    public GameObject target;
    public float fearDistance = 20;
    private Vector3 desiredVelocity;
    private Vehicle m_vehicle;
    private float maxSpeed;

    void Start() {
        m_vehicle = GetComponent<Vehicle> ();
        maxSpeed = m_vehicle.maxSpeed;
    }

    public override Vector3 Force() {
        Vector3 Pos = new Vector3 (transform.position.x, 0, transform.position.z);
        Vector3 targetPos = new Vector3 (target.transform.position.x, 0, target.transform.position.z);
        if (Vector3.Distance (Pos, targetPos) > fearDistance) 
            return new Vector3 (0, 0, 0);
        desiredVelocity = (transform.position - target.transform.position).normalized * maxSpeed;
        return (desiredVelocity - m_vehicle.velocity);
    }
}

行为3:抵达

public class SteeringForArrive : Steering {

    private bool isPlanar = true;
    public float arrivalDistance = 0.3f;
    public float characterRadius = 1.2f;
    public float slowDownDisatance;
    public GameObject target;
    private Vector3 desiredVelocity;
    private Vehicle m_vehicle;
    private float maxSpeed;

    void Start() {
        m_vehicle = GetComponent<Vehicle> ();
        maxSpeed = m_vehicle.maxSpeed;
        isPlanar = m_vehicle.isPlanar;
    }

    public override Vector3 Force() {
        Vector3 toTarget = target.transform.position - transform.position;
        Vector3 returnForce;
        if (isPlanar)
            toTarget.y = 0;
        float distance = toTarget.magnitude;
        if (distance > slowDownDisatance) {
            desiredVelocity = toTarget.normalized * maxSpeed;
            returnForce = desiredVelocity - m_vehicle.velocity;
        } else {
            // ??? cal new return forre  为什么要用距离减去两倍的速度
            desiredVelocity = toTarget - m_vehicle.velocity;
            returnForce = desiredVelocity - m_vehicle.velocity;
        }
        return returnForce;
    }
}

相关文章

  • 操纵行为

    可移动的AI角色基类 AI角色移动控制类 操控行为的基类 (抽象类) 行为1: 靠近 行为2:离开 行为3:抵达

  • 行为操纵术

    从科学诞生以来,人类史上根本性的变革来自于以人为中心,宗教和帝王的统治思想瓦解,另一方面,帝王往往是宗教神灵在世俗...

  • 003《从“为什么”开始》后感+书摘

    影响人的方法——操纵vs感召。操纵就是用降价,恐吓,煽动等方式影响用户的行为,操纵虽然能够在短期奏效,却无法在长期...

  • 期权操纵行为论

    第一,上世纪三十年代初关于期权操纵行为的争论。1934年,美国证券交易委员会(SEC)成立。成立之后,SEC针对当...

  • 正确看待孩子的不良行为

    不良行为是缺乏知识的行为缺乏有效技能行为,因失望产生的行为,被原始脑操纵的行为。 所有人的首要行为目的都是归...

  • 怎么看待市场操控行为?

    在没有政府监管的情况下,市场操纵行为有解决的办法吗? —— sanjin 摘要:市场操纵的历史很久,在金融或是证券...

  • Unity GameAI-(2)Steering---WallA

    操控行为--------避墙 1. 操控行为基本概念 操控行为是指操纵控制角色,让它们能以模拟真实的方式在游戏世界...

  • 第四章 重新看待不良行为 读书笔记

    认识孩子的不良行为是什么。 因失望而产生的行为,缺乏技能的行为,爬行动物脑操纵的行为,发现适应性行为。 一个行为不...

  • 2018-10-11第四章重新看待不良行为

    一.什么是不良行为: 因失望而产生的行为,缺乏技能的行为,爬行动物脑操纵的行为,或者发展适应性行为。 既然我们要孩...

  • 从为什么开始

    从为什么开始 一、为什么要用黄金圈法则来思考? 1. “操纵”的方式 影响别人行为的方式大部分都是“操纵”的方式,...

网友评论

      本文标题:操纵行为

      本文链接:https://www.haomeiwen.com/subject/rlphvftx.html