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  

TriggerEnter


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

[RequireComponent(typeof(Button))]
public class Behaviour : MonoBehaviour
{
  [SerializeField] Button testBtn;
 // Start is called before the first frame update
 void Start()
 {
}
// ゲームオブジェクト同士が接触したタイミングで実行
void OnTriggerEnter(Collider other)
{
 // もし衝突した相手オブジェクトの名前が"Cube"ならば
  if (other.name == "Cube")
{
// 衝突した相手オブジェクトのmaterialの色を黒に変更する
 other.GetComponent<Renderer>().material.color = Color.black;
 }
}
 // ゲームオブジェクト同士が接触から離れたタイミングで実行
void OnTriggerExit(Collider other)
{
// もし接触から離れた相手オブジェクトの名前が"Cube"ならば
  if (other.name == "Cube")
  {
// 接触から離れた相手オブジェクトののmaterialの色をblueに変更する
 other.GetComponent<Renderer>().material.color = Color.blue;
  }
 }

//  void Hoge()
//  {
// this.transform.position += new Vector3(0, 0, 0.005f);
// GetComponent<Rigidbody>().AddForce(0, 0, 0.002f, ForceMode.Impulse);
 //  }

// Update is called once per frame
 void Update()
{
 // var button = GetComponent<Button>();
 // button.onClick.AddListener(OnClickButton2);
testBtn.onClick.AddListener(() => GetComponent<Rigidbody>().
AddForce(0, 0, 0.002f, ForceMode.Impulse));
 // testBtn.onClick.AddListener(Hoge);
 // このオブジェクトのTransformコンポーネントのPositionの値を変更する
// Update()が実行される度に、Z座標の値を+0.01する
 //  this.transform.position += new Vector3(0, 0, 0.01f);
 }
}
///////////////////////////////////////