MyUnityTest

 2023.10.12 unityC#  
 2023.09.18 unityC#  
 2023.09.18 unityC#  
 2023.09.15 unityC#  
 2023.09.15 unityC#  
 2023.09.15 unityC#  
 2023.09.14 unityC#  
 2023.09.13 unityC#  
 2023.09.13 unityC#  
  • Home
  •  > 
  • unityC#

unityC#カテゴリー記事の一覧です

SoundOnOff-Slider


using UnityEngine;
using UnityEngine.UI;
public class SoundPlay : MonoBehaviour
{
    AudioSource audioSource;
    private bool toggle;
    public Slider slider;
    void Start()
 {
   audioSource = GetComponent();
   toggle = true;    //初期設定
   slider.onValueChanged.AddListener(value => 
   this.audioSource.volume = value);
 }
//button onClick
    public void PlayStart()
    {
        if (toggle)
        {
            audioSource.Play();
            toggle = false;
        }
        else
        {
            audioSource.Stop();
            toggle = true;
        }
    }
}

JoystickTest


//playerに
using UnityEngine;
public class MovingObj : MonoBehaviour
{
  //プレイヤーのスピード 
   public float joystickMoveSpeed = 5f;  
 //インスペクターから『Fixed Joystick』を追加。
    public Joystick joystick; 
  //ジョイスティックの傾き度を取得
    Vector3 joystickMoveVector; 
    private Animator anim;
    private Rigidbody2D rb;
    void Start()
    {
     anim = GetComponent();
     //  rigid = GetComponent();
     rb = GameObject.Find("Player").GetComponent();
    }
    void Update()
    {
     if (!Input.GetMouseButton(0))
     {
       anim.SetBool("del", false);
        return;
     }
     joystickMoveVector = Vector3.right * joystick.Horizontal + Vector3.up * joystick.Vertical;
   if (joystickMoveVector != Vector3.zero)  //ジョイスティックを動かす
   {
   transform.Translate(joystickMoveVector * joystickMoveSpeed * Time.deltaTime, Space.World); 
  anim.SetBool("del", true);
   GetComponent().flipX = true;
   }
    Vector3 lookDir = new Vector3(joystick.Direction.x, 0, 0);
     transform.right = -lookDir;
    }
}
///////////////////////////
//playerに
using UnityEngine;
public class PlayerAttack : MonoBehaviour
{
    public GameObject prefabBullet;
    public GameObject prefabBullet2;
    public Transform bulletPoint;
    private bool ture;
    public void Attack()
    {
    GetComponent().flipX = false;
     Instantiate(prefabBullet2, bulletPoint.position, bulletPoint.rotation);
 }
    public void Attack2()
 {
 GetComponent().flipX = true;
  Instantiate(prefabBullet, bulletPoint.position, bulletPoint.rotation);
 }
}
///////////////////////////////
//bulletに
using UnityEngine;
public class Bullet : MonoBehaviour
{
  void Update()
  {
  transform.position += new Vector3(-13f * Time.deltaTime, 0, 0);
 }
}
_________________
public class Bullet2 : MonoBehaviour
{
 void Update()
  {
   transform.position += new Vector3(13f * Time.deltaTime, 0, 0);
 }
}

////////////////////
using UnityEngine;
public class Player : MonoBehaviour
{
    public float joystickMoveSpeed = 5f; 
    public Joystick joystick; 
    Vector3 joystickMoveVector; 
void Update()
  {
  if (!Input.GetMouseButton(0))
  {
   return;
   }
  joystickMoveVector = Vector3.zero;
  joystickMoveVector.x = joystick.Horizontal;
  joystickMoveVector.y = joystick.Vertical;
   joystickMoveVector = Vector3.right * joystick.Horizontal + Vector3.up * joystick.Vertical * 1.7f;
  if (joystickMoveVector != Vector3.zero) 
  {
 transform.Translate(joystickMoveVector * joystickMoveSpeed * Time.deltaTime, Space.World);
 }
  else{
    }
  float angle = Mathf.Atan2(joystickMoveVector.y, joystickMoveVector.x) * Mathf.Rad2Deg;
  transform.rotation = Quaternion.Euler(0f, 0f, angle);
  }
}

CameraWithPlayer


//cameraにアタッチ
using UnityEngine;
public class CameraMove : MonoBehaviour
{
 [SerializeField] Transform playerTr; // プレイヤーのTransform
 [SerializeField] Vector3 cameraOrgPos = new Vector3(0, 0, -10f); // カメラの初期位置位置 
Vector2 camaraMaxPos = new Vector2(22, 6); // カメラの右上限界座標
Vector2 camaraMinPos = new Vector2(-22,-6); // カメラの左下限界座標
    void LateUpdate()
 {
  Vector3 playerPos = playerTr.position; // プレイヤーの位置
   Vector3 camPos = transform.position; // カメラの位置
   // 滑らかにプレイヤーの場所に追従
    camPos = Vector3.Lerp(transform.position, playerPos + cameraOrgPos, 3.0f * Time.deltaTime);
   // カメラの位置を制限
   camPos.x = Mathf.Clamp(camPos.x, camaraMinPos.x, camaraMaxPos.x);
   camPos.y = Mathf.Clamp(camPos.y, camaraMinPos.y, camaraMaxPos.y);
   camPos.z = -10f;
   transform.position = camPos;
    }
}
///////////////////////////
using UnityEngine;

public class CameraScript : MonoBehaviour
{
    public GameObject player;
    // Use this for initialization
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
      Vector3 playerPos = player.transform.position;
      //カメラとプレイヤーの位置を同じにする
      transform.position = new Vector3(playerPos.x, playerPos.y, -10);
    }
}

CameraChange


//SubCameraの設定
//SubCanvasの設定(Canvasをコピー)
// GameObjectにアタッチ
using UnityEngine;
using UnityEngine.UIElements;

public class Camera : MonoBehaviour
{
  private GameObject mainCamera;      //メインカメラ格納用
  private GameObject subCamera;       //サブカメラ格納用 
  [SerializeField] GameObject target;   //Playerに設定
  private Vector3 offset;
  void Start()
  {
      mainCamera = GameObject.Find("MainCamera");
      subCamera = GameObject.Find("SubCamera");
      subCamera.SetActive(false);
    }
    void Update()
    {
      Vector3 playerPos = this.target.transform.position;
      if (playerPos.x >= mainCamera.transform.position.x + 8f)
     {
      //サブカメラをアクティブに設定
      mainCamera.SetActive(false);
      subCamera.SetActive(true);
  }
    else if(playerPos.x >= mainCamera.transform.position.x - 10f)
  {
     //メインカメラをアクティブに設定
     subCamera.SetActive(false);
     mainCamera.SetActive(true);
  }
 }
}

月別
固定ページ