草原历险记 脚本后附 :

widgetcontroller :
{
public float rollSpeed = 0.0f;

public CharacterController controller;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public float fastSpeed = 2.0f;
public float duckSpeed = 0.5f;
public float moveHorz = 0.0f;
public bool isControllable = true;
public widgetstatus widgetstatus;

private Vector3 moveDirection = Vector3.zero;
private Vector3 rotatorDirection = Vector3.zero;
private float duckHeight = 1.0f;
private float normalHeight = 2.0f;
private bool grounded = false;
private bool isBoosting = false;
private bool isDucking = false;
void Start()
{
    controller = GetComponent<CharacterController>();
    widgetstatus = GetComponent<widgetstatus>();
}


void Update()
{
    controller = GetComponent<CharacterController>();
}
private void FixedUpdate()
{
    if (isControllable)
    {
        Input.ResetInputAxes();  重置水平轴
    }
    else
    {
        if (grounded)
        {
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
            moveDirection = new Vector3(h, 0, v);
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= rollSpeed;
            moveHorz = Input.GetAxis("Horizontal");
            if (moveHorz > 0)
            {
                rotatorDirection = new Vector3(0, 1, 0);
            }
            else if (moveHorz < 0)
            {
                rotatorDirection = new Vector3(0, 1, 0);
            }
            else
            {
                rotatorDirection = new Vector3(0, 0, 0);
            }
            if (Input.GetButton("jump"))
            {
                moveDirection.y = jumpSpeed;
            }
            if (Input.GetButton("Boost"))
            {
                if (widgetstatus.energy > 0)
                {
                    moveDirection *= fastSpeed;
                    widgetstatus.energy = widgetstatus.widgetBoostUsage * Time.deltaTime;
                    isBoosting = true;
                }
            }
            if (Input.GetButton("Boost"))
            {
                isBoosting = false;
            }
            if (Input.GetButton("Duck"))
            {
                controller.height = duckHeight;
                controller.center = new Vector3(controller.center.x, controller.height / 2 + 0.2f, controller.center.z);
                moveDirection *= duckSpeed;
                isDucking = true;
            }
            if (Input.GetButton("Duck"))
            {
                controller.height = normalHeight;
                controller.center = new Vector3(controller.center.x, controller.height * 2, controller.center.z);
                moveDirection *= rollSpeed;
                isDucking = false;
            }
            if (Input.GetKeyUp(KeyCode.P))
            {
                widgetstatus.ApplyDamage(3);
            }
            if (Input.GetKeyUp(KeyCode.O))
            {
                widgetstatus.AddHealth(3);
            }

            moveDirection.y = gravity * Time.deltaTime;
            CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
            controller.transform.Rotate(rotatorDirection * Time.deltaTime);
            grounded = ((flags & CollisionFlags.CollidedBelow) != 0);
        }
    }
}

}

widgetstatus :

public class widgetstatus : MonoBehaviour
{
public float health=10.0f;
public float damage ;
public float maxHealth = 10.0f;
public float energy = 10.0f;
public float maxenergy = 10.0f;
public float widgetBoostUsage = 5.0f;
public AudioClip hitsound;
public AudioClip deathsound;

public WidgetController playerController;
public WidgetAnimiation animationState;
public CharacterController controller;
void Start()
{
    playerController = GetComponent<WidgetController>();
    animationState = GetComponent<WidgetAnimiation>();
    controller = GetComponent<CharacterController>();
}


void Update()
{
    
}
public void AddHealth(float boost) 
{
    health += boost;
    if (health > maxHealth) 
    {
        health = maxHealth;
    }
}
public void ApplyDamage(float boost) 
{
    health -= damage;
    if (hitsound) 
    {
        GetComponent<AudioSource>().clip = hitsound;
        GetComponent<AudioSource>();
    }
    if (health <= 0) 
    {
        health = 0;
        StartCoroutine (Die());
    }
}
public void Addenergy(float boost) 
{
    energy += boost;
    if (energy >= maxenergy) 
    {
        energy = maxenergy;
    }
}

IEnumerator Die()
{

    //Debug.Log("呜呜,挂掉了!!");
    if (deathsound) 
    {
        GetComponent<AudioSource>().clip = deathsound;
        GetComponent<AudioSource>();
    }
    playerController.isControllable = false;

    animationState.PlayDie();

    yield return StartCoroutine(WaitForDie());
    
    HideCharacter();

    yield return StartCoroutine((string)WaitForOneSeconds());
    if (checkpoint.isActivept) 
    {
        controller.transform.position = checkpoint.isActivept.transform.position;
        controller.transform.position = new Vector3(controller.transform.position.x,controller.transform.position.y+0.5f,controller.transform.position.z);
        
    }

    ShowCharacter();
    animationState.ReBorn();
    
    health = maxHealth;
}
IEnumerator WaitForDie() 
{
    yield return new WaitForSeconds(3.5f);
}
IEnumerable WaitForOneSeconds() 
{
    yield return new WaitForSeconds(1.0f);
}
void HideCharacter() 
{
    GameObject.FindGameObjectWithTag("Body").GetComponent<SkinnedMeshRenderer>().enabled=false;
    GameObject.FindGameObjectWithTag("Wheels").GetComponent<SkinnedMeshRenderer>().enabled = false;
    playerController.isControllable = true;
}
void ShowCharacter() 
{
    
}

}

camera :
{
public Transform target;
public float distance = 10.0f;
public float height = 5.0f;
public float heightDamping = 2.0f;
public float rotatorDamping = 3.0f;
public float distanceDampingX = 0.5f;
public float distanceDampingZ = 0.2f;
public float camSpeed = 2.0f;
public bool smoothed = true;
private float wantedRotationAngle;
private float wantedHeight;
private float wantedDistanceZ;
private float wantedDistanceX;
private float currentRotationAngle;
private float currentHeight;
private float currentDistanceZ;
private float currentDistanceX;
private Quaternion currentRotation;
private void LateUpdate()
{
if (!target)
{
return;
}
wantedRotationAngle = target.eulerAngles.y;
wantedHeight = target.position.y + height;
wantedDistanceZ = target.position.z - distance;
wantedDistanceX = target.position.x - distance;

    currentRotationAngle = transform.eulerAngles.y;
    wantedHeight = transform.position.y;
    wantedDistanceZ = transform.position.z;
    wantedDistanceX = transform.position.x;
   
    currentRotationAngle= Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotatorDamping*Time.deltaTime);
    wantedHeight = Mathf.LerpAngle(currentHeight, wantedHeight, heightDamping*Time.deltaTime);
    wantedDistanceZ = Mathf.LerpAngle(currentDistanceZ, wantedDistanceZ, distanceDampingZ * Time.deltaTime);
    currentDistanceX = Mathf.LerpAngle(currentDistanceX,wantedDistanceX,distanceDampingX*Time.deltaTime);
    
    currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
    transform.position = currentRotation * Vector3.forward * distance;
    transform.position = new Vector3(currentDistanceX, currentHeight, currentDistanceZ);

   LookAtMe();
    void LookAtMe() 
    {
        if (smoothed)
        {
            Quaternion camRotator = Quaternion.LookRotation(target.position - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, camRotator, Time.deltaTime);

        }
        else 
        {
            transform.LookAt(target);
        }
        
    }
}

widgetanimation :
public class WidgetAnimation : MonoBehaviour
{

public WidgetController playercontroller;
public Animator animator;
void Start()
{
    playercontroller = GetComponent<WidgetController>();
    animator = GetComponent<Animator>();
    if (animator.layerCount == 2) 
    {
        animator.SetLayerWeight(1,1);
    }
}


void Update()
{
    if (playercontroller.isground())
    {
        animator.SetBool("isfalldown", false);

        if (playercontroller.IsBoosting())
        {
            animator.SetBool("isBoost", true);
        }
        else
        {
            animator.SetBool("isBoost", false);
        }
        if (playercontroller.IsDucking())
        {
            animator.SetBool("isducking", true);
        }
        else
        {
            animator.SetBool("isducking", false);
        }
        if (playercontroller.Ismoving())
        {
            animator.SetFloat("Speed", Input.GetAxis("Vertical"));
        }
    }
    else 
    {
        if (Input.GetButtonDown("jump"))
        {
            animator.SetBool("isjump",true);
        }
        if (Input.GetButtonUp("jump")) 
        {
            animator.SetBool("isjump",false);
        }
        if (playercontroller.isground()) 
        {
            animator.SetBool("isfalldown", true);
        }
    }
}
public void GetHit() 
{
    animator.SetBool("isgothit",true);
}
public void PlayDie() 
{
    animator.SetBool("isdead",true);
}
public void ReBorn() 
{
    animator.SetBool("isDead",false);
}

}

widgetinventory :
public class widgetinvet : MonoBehaviour
{
public enum inventoryItem
{
ENERYPACK,REPAIRKIT
}
public widgetstatus playerstatus;
public float repairKitHealamt = 5.0f;
public float energyPackHealAmt = 5.0f;
Dictionary<inventoryItem, int> widgetDict;
void Start()
{
playerstatus = GetComponent();
widgetDict = new Dictionary<inventoryItem, int>();

    widgetDict.Add(inventoryItem.ENERYPACK, 1);
    widgetDict.Add(inventoryItem.REPAIRKIT, 2);
}


public void GetItem(inventoryItem item,int amount) 
{
    widgetDict[item] += amount;
    print(widgetDict[item]);
}
public void UseItem(inventoryItem item, int amount) 
{
    if (widgetDict[item] <= 0) 
    {
        return;
    }
    widgetDict[item] -= amount;
    switch (item) 
    {
        case inventoryItem.ENERYPACK :
            playerstatus.Addenergy(energyPackHealAmt);
            break;
        case inventoryItem.REPAIRKIT:
            playerstatus.AddHealth(repairKitHealamt);
            break;

    }
}
public bool compareItemCount(inventoryItem compItem, int compNumber) 
{
    return widgetDict[compItem] >= compNumber;
}
public int GetItemCount(inventoryItem compItem) 
{
    return widgetDict[compItem];
} 

}

checkpoint :
public class checkpoint : MonoBehaviour
{
public static checkpoint isActivept;
public checkpoint firstpt;
public widgetstatus playerstatus;

void Start()
{

    playerstatus = GameObject.FindWithTag("Player").GetComponent<widgetstatus>();
    isActivept = firstpt;
}

void Update()
{

}
private void OnTriggerEnter(Collider collider)
{
    if (isActivept != this) 
    {
        isActivept = this;
    }
    playerstatus.AddHealth(playerstatus.maxHealth);
    playerstatus.Addenergy(playerstatus.maxenergy);
}

}
pickuptem :
public class pickuptem : MonoBehaviour
{
public widgetinvet.inventoryItem itemType;
public int itemAmount = 1;
public bool pickedup = false;
private void OnTriggerEnter(Collider collider)
{
if (pickedup)
return;
widgetinvet widgetinvet = collider.GetComponent();
widgetinvet.GetItem(itemType,itemAmount);
pickedup = true;
Destroy(gameObject);
}
}

Enemystatus :
public class status : MonoBehaviour
{
public float health = 10.0f;

public bool isDead = false;
void Start()
{
    
}


public void ApplyDamage(float damage) 
{
    if (health <= 0) 
    {
        return;
    }
    health -= damage;

    GetComponent<Animation>().Play("EBunnt_Hit");

    if (health <= 0 && !isDead) 
    {
        health = 0;
        isDead = true;
        StartCoroutine(Die());
    }
}

IEnumerator Die() 
{
    GetComponent<Animation>().Stop();
    GetComponent<Animation>().Play("EBunny_Death");
    Destroy(this.GetComponent<status>());
    yield return new WaitForSeconds(2.0f);
    Destroy(this.gameObject);
}

public bool IsDead() 
{
    return isDead;
}   

}

AIcontroller :
public class AIController : MonoBehaviour
{
public Transform target;
public float rotateSpeed = 30.0f;
public float walkSpeed = 3.0f;
public float attackMoveSpeed = 5.0f;
public float attackDistance = 15.0f;
public float attackRadius = 2.5f;
public float damage = 1.0f;

public bool isAttacking = false;
public float directionTraveltime = 2.0f;
public float idletime = 0.5f;
public Vector3 attackPosition = new Vector3(0, 1, 0);

private CharacterController characterController;
private float timeToNewDirection = 0.0f;
private Vector3 distanceToPlayer;
private float lastAttackTime = 0.0f;
void Start()
{
    characterController = GetComponent<CharacterController>();
    if (!target)
    {
        target = GameObject.FindWithTag("Player").transform;
    }
    GetComponent<Animation>().wrapMode = WrapMode.Loop;
    GetComponent<Animation>()["EBunny_Death"].wrapMode = WrapMode.Once;
    GetComponent<Animation>()["EBunny_Death"].layer = 5;
    GetComponent<Animation>()["EBunny_Hit"].layer = 3;
    GetComponent<Animation>()["EBunny_Attack"].layer = 1;

    StartCoroutine(InitEnemy());
}
IEnumerator InitEnemy()
{
    while (true)
    {
        yield return StartCoroutine(Idle());
        yield return StartCoroutine(Attack());
    }
}

IEnumerator Idle()
{
    while (true)
    {
        if (Time.time > timeToNewDirection)
        {
            yield return new WaitForSeconds(idletime);
            if (Random.value > 0.5)
            {
                transform.Rotate(new Vector3(0, 5, 0), rotateSpeed);
            }
            else
            {
                transform.Rotate(new Vector3(0, -5, 0), rotateSpeed);
            }
            timeToNewDirection = Time.time + directionTraveltime;
        }
        Vector3 walkForward = transform.TransformPoint(attackPosition) - target.position;
        if (Time.time > lastAttackTime+2.0f&&location.magnitude<attackRadius) 
        {
            target.SendMessage("ApplyDamage",damage);
            lastAttackTime = Time.time;
        }
        if (location.magnitude > attackRadius) 
        {
            lostSight = true;
            yield break;
        }
        yield return null;
    }

    isAttacking = false;
}

}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗪磕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值