Unity-C#

 2024.07.07 unityC#  
 2024.05.15 unityC#  
 2024.05.15 unityC#  
 2022.11.06 unityC#  
 2022.11.06 unityC#  
 2022.11.06 unityC#  
 2022.11.06 unityC#  
 2022.11.06 unityC#  
 2022.08.16 spritekit  

CollisionEnter


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[RequireComponent(typeof(Button))]
//Sphere tach
public class Behaviour : MonoBehaviour
{
  [SerializeField] Button testBtn;
  Rigidbody rb;
   // Start is called before the first frame update
  void Start()
   {
    rb = GetComponent<Rigidbody>(); 
 }
// ゲームオブジェクト同士が接触したタイミングで実行
 void OnCollisionEnter(Collision collision)
  {
  // もし衝突した相手オブジェクトの名前が"Cube"ならば
   if (collision.gameObject.name == "Cube")
 {
    // 衝突した相手オブジェクトを削除する
    Destroy(collision.gameObject);
   GetComponent<Renderer>().material.color = Color.blue;
    rb.isKinematic = true;
    }
 }
// ゲームオブジェクト同士が接触している間、持続的に実行
 void OnCollisionStay(Collision collision)
{
  // もし接触している相手オブジェクトの名前が"Plane"ならば
if (collision.gameObject.name == "Plane")
 {
// このオブジェクトのRendererコンポーネントの
//materialの色をランダムに変更する
// new Color(赤:0~1, 青:0~1, 緑:0~1, 透明度:0~1);
// GetComponent<Renderer>()
// .material.color = new Color(Random.value, Random.value, Random.value, 1.0f);
 }
}

// ゲームオブジェクト同士が接触から離れたタイミングで実行
void OnCollisionExit(Collision collision)
 {
// もし接触から離れた相手オブジェクトの名前が"Plane"ならば
 if (collision.gameObject.name == "Plane")
 {
// このオブジェクトのmaterialの色を白に変更する
// GetComponent<Renderer>().material.color = Color.blue;
// GetComponent<Rigidbody>().AddForce(0, 0, 0, ForceMode.Impulse);
  }
}
void Hoge()
 {
GetComponent<Rigidbody>().AddForce(0, 0, 0.009f, ForceMode.Impulse);
}
// Update is called once per frame
 void Update()
 {
 testBtn.onClick.AddListener(Hoge);
}
}
/////////////////////////////////