游戏蛮牛学习群(纯技术交流,不闲聊):159852603
游戏蛮牛 手机端
开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   碎碎

捕鱼中枪的旋转效果,点击发射子弹 热度 1

个人分类: 鼠标控制 | 2018-7-10 11:20
1 个评论 | 阅读 1769 | 收藏 | 举报

1.3D场景中,2018.7.10

[code]csharpcode:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 控制手枪的旋转
/// 
/// 控制子弹的生成
/// 
/// 控制子弹的移动
/// </summary>
public class GunManager : MonoBehaviour {
    //枪击音效
    private AudioSource gunAudio;
    //手枪在X,Y方向上旋转的最大值和最小值
    private float maxYRotation = 120;
    private float minYRotation = 0;
    private float maxXRotion = 60;
    private float minXRotation = 0;
    //射击的间隔时长
    private float shootTime = 1;
    //射击间隔时间计时器
    private float shootTimer = 0;
    //子弹的游戏物体
    public GameObject bulletGO;
    //子弹的生成位置
    public Transform firePos;

	// Use this for initialization
	void Awake () {
        gunAudio = gameObject.GetComponent<AudioSource>();
	}

    // Update is called once per frame
    void Update()
    {
        //游戏非暂停状态下,射击并且枪随鼠标旋转
        if(GameManager._instance.isPaused == false)
        {
            shootTimer += Time.deltaTime;
            if(shootTimer >= shootTime)
            {
                //射击
                if(Input.GetMouseButtonDown(0))
                {
                    //实例化,生成子弹
                    GameObject bullectCurrent = GameObject.Instantiate(bulletGO, firePos.position, Quaternion.identity);
                    //给子弹添加一个正前方向上的力,向前运动
                    bullectCurrent.GetComponent<Rigidbody>().AddForce(transform.forward * 2200);
                    //射击时手枪动画
                    gameObject.GetComponent<>().Play();
                    shootTimer = 0;
                    //播放
                    gunAudio.Play();
                    //UI显示射击数
                    UIManager.instance.AddShootNum();
                }
            }
            //获得鼠标的相对位置
            float xPosPrecent = Input.mousePosition.x / Screen.width;
            float yPosPrecent = Input.mousePosition.y / Screen.height;
            //x,y角度计算
            float xAngle = -Mathf.Clamp(yPosPrecent * maxXRotion, minXRotation, maxXRotion) + 15;
            float yAngle = Mathf.Clamp(xPosPrecent * maxYRotation, minYRotation, maxYRotation) - 60;
            //赋值,手枪旋转
            transform.eulerAngles = new Vector3
                (xAngle, yAngle, 0);
        }
    } 
}

[code]csharpcode:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 子弹销毁
/// </summary>
public class BulletManager : MonoBehaviour {

	// Use this for initialization
	void Start () {
        StartCoroutine(DestoryBullet());
	}
    //2秒后销毁子弹
    IEnumerator DestoryBullet()
    {
        yield return new WaitForSeconds(2);
        Destroy(this.gameObject);
    }
}
2.2D场景中,2018.9.4

[code]csharpcode:

public class GunFollow : MonoBehaviour
{
    public RectTransform UGUICanvas;
    public Camera mainCamera;
    void Update()
    {
        Vector3 mousePos;

        RectTransformUtility.ScreenPointToWorldPointInRectangle(UGUICanvas, new Vector2(Input.mousePosition.x, Input.mousePosition.y), mainCamera, out mousePos);
        float z;
        if(mousePos.x > transform.position.x)
        {
            z = -Vector3.Angle(Vector3.up, mousePos - transform.position);
        }
        else
        {
            z = Vector3.Angle(Vector3.up, mousePos - transform.position);
        }
        transform.localRotation = Quaternion.Euler(0, 0, z);
    }
}
1 0

作者的其他最新博客

评论 (1 个评论)

回复 zwfjlyfs 2018-7-10 12:17
谢谢分享。多谢多谢

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

个人分类

标签

阅读排行

评论排行

推荐博客

最新博客

关闭

站长推荐 上一条 /1 下一条

返回顶部