天涯明月刀ol|天涯明月刀迅雷下载
  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));
}
}
 
 
}
--------------------- 
 
捕魚駕到 天涯明月刀ol