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  
  • Home
  •  > 
  • 2022年

page.phpテンプレートを利用しています。

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);
 }
}
///////////////////////////////////////

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);
}
}
/////////////////////////////////

TextObject


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

public class MyText : MonoBehaviour
{
 bool m_xPlus = true;
public GameObject score_object = null;
// Textオブジェクト
public float score_num = 0; // スコア変数
void Start()
    {
//DelayMethodを2秒後に呼び出し、以降は1秒毎に実行
 InvokeRepeating(nameof(DelayMethod), 2.0f, 1.0f);
  }
 void DelayMethod()
 {
Text score_text = score_object.GetComponent<text>();
score_text.color = new Color(1.0f, 0.0f, 0.0f, 1.0f);
score_text.fontSize = 80;
 // テキストの表示を入れ替える
  score_text.text = "Score:" + score_num;
   if (m_xPlus)
   {
      score_num += 1;
     if (score_num >= 20)
       m_xPlus = false;
  }
    else
  {
    score_num -= 1;
   if (score_num <= 0)
    m_xPlus = true;
    }
 }
void Update()
 {      
 }
}
////////////////////////////////

MouseDrag


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

public class Player : MonoBehaviour
{
 private Vector3 screenPoint;
  void OnMouseDrag()
 {
 screenPoint = Camera.main.WorldToScreenPoint(transform.position);
float screenX = Input.mousePosition.x;
// float screenY = Input.mousePosition.y;
float screenY = screenPoint.y;
 float screenZ = screenPoint.z;
 Vector3 currentScreenPoint = new Vector3(screenX, screenY, screenZ);
 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint);
transform.position = currentPosition;
}
}
///////////////////////////////