Unityスクリプト集
unity2017 1 1のバージョンしか動かないよー
https://unity3d.com/jp/get-unity/download/archive
プレイヤースクリプト改良編
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerContlloer : MonoBehaviour {
Animator anim;
public float speed = 10.0F;
public float rotationSpeed = 10.0F;
public Sword sword;
public GameObject model;
public int Health = 1;
public Slider healthbar;
bool is_death;
private Rigidbody PlayerRigibody;
public int level;
public float experlence;
public float experlenceRequlred;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
PlayerRigibody = GetComponent<Rigidbody> ();
Cursor.lockState = CursorLockMode.Locked;
level = 1;
Health = 100;
experlence = 0;
experlenceRequlred = 100;
}
// Update is called once per frame
void Update () {
Exp();
float translation = Input.GetAxis ("Vertical") * speed;
float straffe = Input.GetAxis ("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *=Time.deltaTime;
transform.Translate(straffe,0,translation);
if(Input.GetMouseButton(0))
{
anim.SetBool("is_attack",true);
sword.Attack();
}
else
anim.SetBool("is_attack",false);
if(translation != 0)
{
anim.SetBool("is_running",true);
anim.SetBool("is_Idle",false);
}
else{
anim.SetBool("is_running",false);
anim.SetBool("is_Idle",true);
}
transform.Rotate(0,Input.GetAxis("Horizontal") * 3,0);
if (Input.GetKeyDown("escape"))
Cursor.lockState = CursorLockMode.None;
}
public virtual void Hit(){
Health--;
healthbar.value = Health;
if (Health <= 0) {
anim.SetBool("is_death",true);
Death();
}
}
public void OnTriggerEnter(Collider otherCollider){
if(otherCollider.GetComponent<Sword>()!=null){
if (otherCollider.GetComponent<Sword> ().IsAttacking) {
Hit ();
}
}
}
public void Death(){
is_death = true;
}
public void RankUp(){
level += 1;
experlence = 0;
switch (level) {
case 2:
Health = 200;
experlenceRequlred = 200;
break;
case 3:
Health = 300;
experlenceRequlred = 300;
print ("congratulations! you have hit level3 on your character!");
break;
}
}
void Exp(){
if (experlence >= experlenceRequlred)
RankUp ();
}
}
敵スクリプト改良編
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class EnemyAI5 : MonoBehaviour {
public Transform Player;
static Animator anim;
public float totalHealth;
public float currentHealth;
public float expGranted;
public float attackDamege;
public float attackSpeed;
public float movementSpeed;
private Playerstats thePlayerstats;
private PlayerContlloer theplayercontlloer;
public Sword sword;
public int health = 1;
public Slider healthbar;
bool is_death;
public float experlence;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
theplayercontlloer = FindObjectOfType<PlayerContlloer> ();
}
// Update is called once per frame
void Update () {
Vector3 direction = Player.position - this.transform.position;
float angle = Vector3.Angle (direction, this.transform.forward);
if(Vector3.Distance(Player.position,this.transform.position) < 10 && angle < 30)
{
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);
anim.SetBool ("is_Idle", false);
if(direction.magnitude > 5)
{
this.transform.Translate (0, 0, 0.05f);
anim.SetBool ("is_walk", true);
anim.SetBool ("is_attack", false);
}
else{
anim.SetBool ("is_attack", true);
anim.SetBool ("is_walk", false);
sword.Attack();
}
}
else{
anim.SetBool ("is_Idle", true);
anim.SetBool ("is_walk", false);
anim.SetBool ("is_attack", false);
}
}
public void GetHit(float damage){
anim.SetInteger("is_damage",3);
health--;
StartCoroutine(RecoverFromHit());
}
IEnumerator RecoverFromHit()
{
yield return new WaitForSeconds (0.1f);
anim.SetInteger("is_damage", 0);
}
public virtual void Hit(){
health--;
anim.SetInteger("is_damage",3);
healthbar.value = health;
if (health <= 0) {
anim.SetBool("is_death",true);
Death();
}
}
public void OnTriggerEnter(Collider otherCollider){
if(otherCollider.GetComponent<Sword>()!=null){
if (otherCollider.GetComponent<Sword> ().IsAttacking) {
Hit ();
}
}
}
public void Death(){
theplayercontlloer.RankUp();
experlence += 100;
is_death = true;
Destroy (gameObject, 2f);
}
基本アズセットの3rdパーソンコントローラーで動くスクリプトアニメーターは3rdパーソン
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
[Header("Visuals")]
public GameObject model;
public float rotatingSpeed = 2f;
public Animator playerAnimator;
[Header("Movement")]
public float movingVelocity;
public float JumpingVelocity;
[Header("Equipment")]
public Sword sword;
public Bow bow;
public int arrowAmount=15;
public GameObject bombPrefab;
public int bombAmount =5;
public float throeingSpeed;
private Rigidbody PlayerRigibody;
private bool canJump;
private Quaternion targetModelRotation;
// Use this for initialization
void Start () {
PlayerRigibody = GetComponent<Rigidbody> ();
targetModelRotation = Quaternion.Euler (0, 0, 0);
}
// Update is called once per frame
void Update () {
//Raycast to identify if the player
RaycastHit hit;
if(Physics.Raycast (transform.position, Vector3.down, out hit, 1.31f)){
canJump=true;
}
playerAnimator.SetBool ("OnGround", canJump);
Debug.Log (canJump);
model.transform.rotation = Quaternion.Lerp (model.transform.rotation, targetModelRotation, Time.deltaTime * rotatingSpeed);
ProcessInput ();
}
void ProcessInput (){
PlayerRigibody.velocity = new Vector3 (
0,
PlayerRigibody.velocity.y,
0
);
//Move in the xz plane.
bool isPlayerMoving = false;
if (Input.GetKey ("right")){
PlayerRigibody.velocity = new Vector3 (
movingVelocity,
PlayerRigibody.velocity.y,
PlayerRigibody.velocity.z
);
targetModelRotation = Quaternion.Euler (0, 90, 0);
isPlayerMoving = true;
}
if (Input.GetKey ("left")){
PlayerRigibody.velocity = new Vector3 (
-movingVelocity,
PlayerRigibody.velocity.y,
PlayerRigibody.velocity.z
);
targetModelRotation = Quaternion.Euler (0, 270, 0);
isPlayerMoving = true;
}
if (Input.GetKey ("up")){
PlayerRigibody.velocity = new Vector3 (
PlayerRigibody.velocity.x,
PlayerRigibody.velocity.y,
movingVelocity
);
targetModelRotation = Quaternion.Euler (0, 0, 0);
isPlayerMoving = true;
}
if (Input.GetKey ("down")){
PlayerRigibody.velocity = new Vector3 (
PlayerRigibody.velocity.x,
PlayerRigibody.velocity.y,
-movingVelocity
);
targetModelRotation = Quaternion.Euler (0, 180, 0);
isPlayerMoving = true;
}
playerAnimator.SetFloat("Forward",isPlayerMoving ? 1f:0f);
//check for jumps.
if (canJump && Input.GetKeyDown("space")) {
canJump = false;
PlayerRigibody.velocity = new Vector3 (
PlayerRigibody.velocity.x,
JumpingVelocity,
PlayerRigibody.velocity.z
);
}
//*check equipment interaction
if (Input.GetKeyDown("z"))
{
sword.gameObject.SetActive (true);
bow.gameObject.SetActive (false);
sword.Attack();
}
if (Input.GetKeyDown ("x")) {
if (arrowAmount > 0) {
sword.gameObject.SetActive (false);
bow.gameObject.SetActive (true);
bow.Attack ();
arrowAmount--;
}
}
if (Input.GetKeyDown ("c")) {
ThrowBomb ();
}
}
private void ThrowBomb(){
if (bombAmount <= 0) {
return;
}
GameObject bombObject = Instantiate (bombPrefab);
bombObject.transform.position = transform.position + model.transform.forward;
Vector3 throwingDirection = model.transform.forward +Vector3.up;
bombObject.GetComponent<Rigidbody> ().AddForce (throwingDirection * throeingSpeed);
bombAmount--;
}
}
}