子弹的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullect : MonoBehaviour {
public float moveSpeed = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(transform.right * moveSpeed * Time.deltaTime);
}
}
人物移动的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum PlayerState
{
PlayerGround,
PlayerDown,
PlayerJump
}
public class PlayerMove : MonoBehaviour {
public float speed = 3;
private Rigidbody rigidbody;
public float jumpSpeed = 3;
public PlayerState state = PlayerState.PlayerJump;
private bool isGround = false;
private int groundLayerMask;
private bool isBottomKeyClick = false;
public PlayerGround playerGround;
public PlayerDown playerDown;
public PlayerJump playerJump;
public GameObject bullectPrefab;
private float timeVal;
private Vector3 bullectEulerAngles;
public Transform shoot;
public Transform Bullect;
void Start()
{ rigidbody=GetComponent<Rigidbody>();
groundLayerMask = LayerMask.GetMask("Ground");
}
// Update is called once per frame
void Update () {
Attack();
if (Input.GetKeyDown(KeyCode.S))
{
isBottomKeyClick = true;
}
if (Input.GetKeyUp(KeyCode.S))
{
isBottomKeyClick = false;
}
float h = Input.GetAxis("Horizontal");
Vector3 v = rigidbody.velocity;
rigidbody.velocity = new Vector3(h * speed, v.y, v.z);
v = rigidbody.velocity;
//判断是否在地面上
RaycastHit hitinfo;
isGround = Physics.Raycast(transform.position + Vector3.up * 0.1f, Vector3.down, out hitinfo, 0.2f, groundLayerMask);
//判断当前主角的状态 跳起 蹲下 正常
if (isGround == false)
{
state = PlayerState.PlayerJump;
}
else
{
if (isBottomKeyClick)
{
state = PlayerState.PlayerDown;
}
else
{
state = PlayerState.PlayerGround;
}
}
//控制主角的跳跃
if (isGround && Input.GetKeyDown(KeyCode.K))
{
rigidbody.velocity = new Vector3(v.x, jumpSpeed, v.z);
}
//根据状态来判定启用哪一个游戏状态
switch (state)
{
case PlayerState.PlayerDown:
playerDown.gameObject.SetActive(true);
playerJump.gameObject.SetActive(false);
playerGround.gameObject.SetActive(false);
break;
case PlayerState.PlayerGround:
playerDown.gameObject.SetActive(false);
playerJump.gameObject.SetActive(false);
playerGround.gameObject.SetActive(true);
break;
case PlayerState.PlayerJump:
playerDown.gameObject.SetActive(false);
playerJump.gameObject.SetActive(true);
playerGround.gameObject.SetActive(false);
break;
}
//控制主角的朝向
float x = 1;
if (rigidbody.velocity.x > 0.05f)
{
x = 1;
bullectEulerAngles = new Vector3(0, 0, 0);
}
else if (rigidbody.velocity.x < -0.05f)
{
x = -1;
bullectEulerAngles = new Vector3(0, 0,-180);
}
else
{
x = 0;
}
if (x != 0)
{
playerGround.transform.localScale = new Vector3(x, 1, 1);
playerJump.transform.localScale = new Vector3(x, 1, 1);
playerDown.transform.localScale = new Vector3(x, 1, 1);
}
//控制主角在idle和walk状态的切换
if (Mathf.Abs(rigidbody.velocity.x) > 0.05f)
{
playerGround.status = AnimStatus.Walk;
playerDown.status = AnimStatus.Walk;
}
else
{
playerGround.status = AnimStatus.Idle;
playerDown.status = AnimStatus.Idle;
}
Attack();
}
//主角的攻击方法
private void Attack()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//子弹产生的角度:当前坦克的角度+子弹应该旋转的角度。
Instantiate(bullectPrefab, transform.position, Quaternion.Euler(transform.eulerAngles + bullectEulerAngles));
timeVal = 0;
}
}
}