unity3d angrybots学习之敌人围绕主角旋转和移动

看过angrybots例子的人都知道,敌人有可能是飞行器,有可能是机器人,这里我们假定他是飞行器,ok,我们首先设定飞行器是rigidbody,然后固定的y的postion,和x,z的rotation,写下如下的代码

using UnityEngine;
using System.Collections;

public class BuzzeMove : MonoBehaviour {

    private Transform player;//目标,target
    private Rigidbody rigidbody;
    public float flyingSpeed = 5.0f;//飞行速度
    public float zigZagSpeed = 2.5f; //设置一个左右的速度
    private Vector3 smootheDirection = Vector3.zero;//使移动更平滑
    private float backtrackIntensity = 0.5f;
    public float oriantationMultiplier = 2.5f;//旋转系数
    // Use this for initialization
    void Awake () {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        rigidbody = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void FixedUpdate () {
        Vector3 direction = player.position - transform.position;
        direction.Normalize();//方向归一化
        smootheDirection = Vector3.Lerp(smootheDirection,direction,Time.deltaTime*3.0f);//差值使得运动平滑
        Vector3 zigzag = transform.right*(Mathf.PingPong(Time.time*zigZagSpeed, 2.0f) - 1.0f) * zigZagSpeed;//给左右加点偏移
        float orientationSpeed = 1.0f;
        Vector3 deltaVelocity = (smootheDirection*flyingSpeed + zigzag) - rigidbody.velocity;
        //如果Vector3.Dot两个值大于0,那么两个对象是面对面的
        if (Vector3.Dot(direction, transform.forward) > 0.8f) {
            rigidbody.AddForce(deltaVelocity, ForceMode.Force);
        }
        else //反之,两个对象该远离了
        {
            rigidbody.AddForce(-deltaVelocity * backtrackIntensity, ForceMode.Force);
            orientationSpeed = oriantationMultiplier;//反方向的时候旋转得快一些
        }

        float rotationAngle = AngleAroundAxis(transform.forward,direction,Vector3.up);
        rigidbody.angularVelocity = Vector3.up * rotationAngle* oriantationMultiplier;//刚体的旋转
        //transform.LookAt(player, Vector3.up);
    }

    // The angle between dirA and dirB around axis
    static float AngleAroundAxis(Vector3 dirA,  Vector3 dirB,  Vector3 axis)
    {
        // Project A and B onto the plane orthogonal target axis
        dirA = dirA - Vector3.Project(dirA, axis);
        dirB = dirB - Vector3.Project(dirB, axis);

        // Find (positive) angle between A and B
        float angle = Vector3.Angle(dirA, dirB);

        // Return angle multiplied with 1 or -1
        return angle * (Vector3.Dot(axis, Vector3.Cross(dirA, dirB)) < 0 ? -1 : 1);
    }
}
时间: 2024-10-01 07:06:43

unity3d angrybots学习之敌人围绕主角旋转和移动的相关文章

定时器实现的地球围绕太阳旋转

一个地球围绕太阳旋转 1 #import "HUAppDelegate.h" 2 3 #define CENTER_X 160 4 #define CENTER_Y 240 5 #define RADIUS 130 6 7 @implementation HUAppDelegate 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)laun

html5 canvas围绕中心点旋转

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

poj 1106(半圆围绕圆心旋转能够覆盖平面内最多的点)

Transmitters Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4955   Accepted: 2624 Description In a wireless network with multiple transmitters sending on the same frequencies, it is often a requirement that signals don't overlap, or at

【程序员编程艺术】学习记录1:左旋转字符串之指针翻转法

[程序员编程艺术]学习记录1:左旋转字符串之指针翻转法 题目:左旋转字符串 定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部,如把字符串abcdef左旋转2位得到字符串cdefab.请实现字符串左旋转的函数,要求对长度为n的字符串操作的时间复杂度为O(n),空间复杂度为O(n) 思路一.暴力移位法 //暴力移位法 void leftshiftone(char *s, int n) { char t = s[0]; for(int i = 1;i < n; i++) s[i-1]

围绕鼠标旋转的炫酷三叶空间旋浆效果

<html><head><title>围绕鼠标旋转的三叶空间旋浆</title><meta content="text/html; charset=gb2312" http-equiv="Content-Type"></head><body bgColor="#000000"><scriptlanguage="JavaScript">

unity3d 触屏多点触控(旋转与缩放)

unity3d 触屏多点触控(旋转与缩放) /*Touch OrbitProgrammed by: Randal J. Phillips (Caliber Mengsk)Original Creation Date: 12/16/2011Last Updated:                   12/16/2011Desctiption: Simple orbit by one touch and drag, as well as pinch to zoom with two finger

【程序员编程艺术】学习记录2:左旋转字符串之循环移位法

[程序员编程艺术]学习记录2:左旋转字符串之循环移位法 GCD算法:(辗转相除法/欧几里得算法) gcd是求最大公约数的算法,作为TAOCP第一个算法 gcd算法流程: 首先给定两个整数m,n(m大于等于n)如果小于则直接交换再处理 ①求余数 r=m%n ②假如r=0,算法结束,n即为所求 否则,重新令m <- n, n <-r 之后循环 <<<<<<<<<<<<<<<<<<<&l

关于unity3D的学习感想

在老师布置团队项目后组长确定项目是做游戏是,我才接触的Unity3D游戏引擎. 因为一开始我没有接触过这类软件,更没有用过.所以作为一个新手,做好的办 法实在网上找教程.网上说Unity3D是由Unity Technologies开发的一个让你轻 松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台 的综合型游戏开发工具,是一个全面整合的专业游戏引擎. 一开始在网上我查的unity圣典社区是专门搞这个的社区 (http://www.ceeger.com/forum/) 于是我就

Unity3D脚本学习——运行时类

AssetBundle 类,继承自Object.AssetBundles让你通过WWW类流式加载额外的资源并在运行时实例化它们.AssetBundles通过BuildPipeline.BuildAssetBundle创建. 参见:WWW.assetBundle ,Loading Resources at Runtime ,BuildPipeline.BuildPlayer function Start () { var www = new WWW ("http://myserver/myBund