Unity_滑鼠左右移動_碰撞球反彈_功能測試_Flag正1過來_負1回去_結果變成擦板球


我們在上一篇文章中主要探討到
一些關於發射的物理射線  和  基本常用I/O操作 、 物件生成之外

我們還發現發射後碰撞地面的未反彈問題
只有擊中目標物功能





這次一樣用 心智圖去擴展解決

第一階段

是想要有一個會跟滑鼠移動的方塊
並且有一顆球體


這裡分享一下視角切換的方法

Step1. 先點選 MainCamera
Step2. 到 GameObject  ---> Align With View



讓他可以左右移動的腳本
我們在上一篇文章分享到








Move.cs
我們讓Cube以 Z軸方向為中心  左右移動 取 10個point位移

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        transform.position = new Vector3(ray.GetPoint(10f).x, 0.0f ,transform.position.z);
    }
}



生出一個大牆壁 記得加 Tag

注意!!!!



Step3.  GameObject ----> Align With View


這邊注意  心智圖  思維

我們場景布局 是需要依照
特定座標位置來設定的



緊接著有球產生(記得加鋼體)
讓球會移動腳本





BallMove.cs 程式
先讓球有移動的速度

寫法一、寫在 Update中


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallMove : MonoBehaviour {
    private Rigidbody rigidbody;

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        this.rigidbody = this.GetComponent<Rigidbody>();
        rigidbody.velocity = new Vector3 (10.0f, 0.0f, 5.0f);
    }
}






寫法二、先寫在start() 之後於 update中不斷呼叫

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallMove : MonoBehaviour {
    private Rigidbody rigidbody;

    // Use this for initialization
    void Start () {
        this.rigidbody = this.GetComponent<Rigidbody>();
        rigidbody.velocity = new Vector3 (10.0f, 0.0f, 5.0f);
    }
    
    // Update is called once per frame
    void Update () {
        Start();
    }
}


效果





之後添加碰撞方法

這裡我故意拉大兩個牆壁版
各自都用  Tag 去辨別



程式碼
想法.    球一開始過來的方向朝 playerCube  設定一個Flag為1

當打中 playerCube 之後  Flag 乘上 -1 反向彈回


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallMove : MonoBehaviour {
    private Rigidbody rigidbody;
    int Invert;
    // Use this for initialization
    void Start () {
        Invert = 1;
    }

    // Update is called once per frame
    void Update () 
    {
        this.rigidbody = this.GetComponent<Rigidbody>();
        rigidbody.velocity = new Vector3 (10.0f, 0.0f, 5.0f);
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "playerCube") {
            Invert = Invert* -1;//反向
            rigidbody.velocity = new Vector3 (10.0f * Invert, 0.0f, -5.0f);
            //rigidbody.velocity = new Vector3 (0.0f, 0.0f, -5.0f);
            //rigidbody.velocity = new Vector3 (10.0f * Invert, 0.0f, -5.0f);
        }
        else if(collision.gameObject.name == "wall") {
            rigidbody.velocity = new Vector3 (10.0f * Invert, 0.0f, -5.0f);
        }
    }
}




後來發現是 名字

不是針對  Tag

= =|||

但效果依然有問題

真是奇怪 = =



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallMove : MonoBehaviour {
    private Rigidbody rigidbody;
    int Invert;
    // Use this for initialization
    void Start () {
        Invert = 1;
    }

    // Update is called once per frame
    void Update () 
    {
        this.rigidbody = this.GetComponent<Rigidbody>();
        rigidbody.velocity = new Vector3 (10.0f, 0.0f, 5.0f);
    }

    void OnCollisionEnter(Collision collision)
    {
        /*物件名字*/

        if (collision.gameObject.name == "playerCube") {
            Invert = Invert* -1;//反向
            rigidbody.velocity = new Vector3 (10.0f * Invert, 0.0f, -5.0f);
            //rigidbody.velocity = new Vector3 (0.0f, 0.0f, -5.0f);
            //rigidbody.velocity = new Vector3 (10.0f * Invert, 0.0f, -5.0f);
        }
        else if(collision.gameObject.name == "wall") {
            rigidbody.velocity = new Vector3 (10.0f * Invert, 0.0f, -5.0f);
        }


        /*tag名稱*/
        /*
        if (collision.gameObject.tag == "playerCube") {
            Invert = Invert* -1;//反向
            rigidbody.velocity = new Vector3 (10.0f * Invert, 0.0f, -25.0f);
            //rigidbody.velocity = new Vector3 (0.0f, 0.0f, -5.0f);
            //rigidbody.velocity = new Vector3 (10.0f * Invert, 0.0f, -5.0f);
        }
        else if(collision.gameObject.tag == "wall") {
            rigidbody.velocity = new Vector3 (10.0f * Invert, 0.0f, -5.0f);
        }
        */

    }
}




兩個都  擦板球效果



= =|||
God



留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients