1. 当前所在位置:
  2. 首页
  3. 游戏打鱼

Unity鼠标控制捕鱼达人炮筒的旋转和发射

2018-12-18 admin
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
 
public class CannonScript : MonoBehaviour {
 
 
public float timer = 0;
public Transform firePoint;
void Start () {
 
}
 
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
//先得到炮筒的位置
Vector3 cannonPos = transform.position;
//得到鼠标点击的位置
Vector3 mousePos = Input.mousePosition;
//把鼠标的位置转成世界坐标
mousePos=Camera.main.ScreenToWorldPoint(mousePos);
//如果按下鼠标左键不松开
if (Input.GetMouseButton (0)) {
//得到大炮到鼠标的方向
Vector2 targetDir = mousePos - cannonPos;
float angle = Vector2.Angle (targetDir, Vector2.up);
//判断angle的值是正还是负
if (mousePos.x > cannonPos.x) {
angle = -angle;
}
//炮筒旋转的方向
transform.eulerAngles = new Vector3 (0, 0, angle);
if (timer >= 0.1f) {
timer = 0;
//产生子弹
FireScript.Create(firePoint.position, new Vector3(0,0,angle));
}
}
 
 
}
--------------------- 
 
捕鱼驾到