2018年6月9日 星期六

Week14_一個上課筆記

作業一 : 期末專題


一、今日目標

今天進度 :
1. 完成各自的分工建模_第二個攤位。
2. 在Unity中,建置射氣球的物理碰撞和程式等。
3. 試寫金魚游動的程式碼。

二、成果

1. 我建置的第二個攤位的模型_投籃機。
這是我做的第二個攤子模型


2. 建置射氣球的物理碰撞和程式
(1)物理碰撞 : 飛鏢本身設置一個碰撞讓手把可以拿起,飛鏢尖端設置空物件放入碰撞,當飛鏢碰到氣球的碰撞,氣球即消失。
(2)程式(套入氣球中)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Ball : MonoBehaviour {
    public Text ScoreBar_balloon;
public GameObject ball; //氣球
void OnTriggerEnter( Collider Ball){
if (Ball.gameObject.tag == "ball1") {  //飛鏢碰到氣球
ball.gameObject.SetActive (false); //氣球消失
     
}

}

}
(3)成果 :


3. 試寫金魚游動的程式碼 :

(1) 金魚不停繞圈的程式 :  (程式碼給金魚)
缺點 : 魚游的方向不對
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

public GameObject fish2; //魚
void Update () {
             fish2.transform.Rotate(Vector3.down*_RotationSpeed,Space.World); 
              //找到魚的中心點,然後自轉
}


}

}
(2) 金魚碰到牆壁的碰撞即轉彎的程式 : (程式碼給金魚)
缺點 : 魚會游出魚缸外
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

public GameObject fish2; //魚
void Update () {

fish2.transform.Translate (0,0,0.5f*Time.deltaTime); //魚向前游

}
void OnTriggerStay(Collider fish){
if (fish.gameObject.tag == "wall") { //當魚碰到牆壁
fish2.transform.Rotate (new Vector3 (0, Time.deltaTime*110f, 0));
       //魚每秒改變110度方向
}

}

}
(3) 老師教的最像金魚游動的程式碼 : (金魚會隨意游動、忽快忽慢)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class fishFlow : MonoBehaviour {
public float fishX, fishY, fishDir, fishDirA, fishSpeed;
public GameObject fish;
public GameObject tank;
float centerX,centerY;
float random(float range){
return Random.Range (0f, range);
}
float angle(){
return Mathf.Atan2 ((fishY - centerY), (fishX - centerX));
}
int frameCount=0;
void draw(){
frameCount++;




fishSpeed = random (tank.transform.localScale.x / 100f);



fishDir += fishDirA;
fishX += fishSpeed * Mathf.Cos (fishDir);
fishY += fishSpeed * Mathf.Sin (fishDir);
fishSpeed = random (3);
if (frameCount % 30 == 0)fishDirA = Mathf.Deg2Rad * (random (2) - 1);

if ((centerX - fishX) * (centerX - fishX) + (centerY - fishY) * (centerY - fishY) < tank.transform.localScale.x * tank.transform.localScale.x) {
float diff = angle () - fishDir;
if (diff < -Mathf.PI * 2)diff += Mathf.PI * 2;
if (diff < 0) { fishDir = angle () + Mathf.PI / 2;fishDirA = Mathf.Deg2Rad * (random (12));}
if (diff > 0) { fishDir = angle () - Mathf.PI / 2;fishDirA = -Mathf.Deg2Rad * (random (12));}
}
//print (Mathf.Rad2Deg * (fishDirA));
}

void initFish(){
centerX = tank.transform.position.x;
centerY = tank.transform.position.y;

float r = random (tank.transform.localScale.x);
float a=random(Mathf.PI*2);
fishX = centerX + r * Mathf.Cos (a);
fishY = centerY + r * Mathf.Sin (a);
fishDir = random (Mathf.PI * 2);
fishDirA = 0;
fishSpeed = random (tank.transform.localScale.x / 100f);
}
// Use this for initialization
void Start () {
initFish ();
}

// Update is called once per frame
void Update () {
draw ();
fish.transform.position = new Vector3 (fishX, fish.transform.position.y, fishY);
}
}

沒有留言:

張貼留言