Unity - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(61评价)
价格: 2208.00元
为什么我怎么调z轴的坐标子弹都是向右边发射不是跟随人物的面朝方向发射
WQ王琪发起了问答2019-12-12
3
回复
281
浏览
子弹的

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;
            
        }
    }


}

 

所有回复
  • 老师_Trigger 2019-12-13

    同学你好,输出一下bullectEulerAngles看看值有没有改变,是不是一直都是0,检查一下相关的逻辑是不是都走else了没有给这个值赋值。

    • WQ王琪 2019-12-13

      老师主要是控制主角朝向问题 人物朝向可以左右行走但是transform面板上的x确实一直不变是为什么 如果要改 该怎么修改我这个代码 我觉得是x值不变导致我的子弹方向无法判定

      (0) 回复
    • 老师_Trigger 2019-12-13

      回复 @ WQ王琪: 老师看同学控制主角朝向是通过Scale去控制的,如果是通过Scale那么我们旋转角度是不会受主角朝向影响的,同学先输出一下看看我们子弹转向的角度有没有问题。

      (0) 回复
    还有-3条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!