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#  

Vector2MoveTest


using UnityEngine;
public class Enemy : MonoBehaviour
{
 Transform playerTr;
 [SerializeField] float speed = 2; 
 private void Start()
 {
   playerTr = GameObject.FindGameObjectWithTag("Player").transform;
 }
 private void Update()
 {
 // プレイヤーとの距離が0.1f未満になったらそれ以上実行しない
 if (Vector2.Distance(transform.position, playerTr.position) < 0.1f)
  return;
// プレイヤーに向けて進む
 transform.position = Vector2.MoveTowards(
  transform.position,
  new Vector2(playerTr.position.x, playerTr.position.y),
  speed * Time.deltaTime);
  }
}
//__________________________________
using UnityEngine;
public class Player : MonoBehaviour
{
    Vector3 startPos;
    Vector3 screenPos;
    Vector3 worldPos;
void Update()
 {
 if (Input.GetMouseButton(0))
  {
   startPos = transform.position;
   screenPos = Input.mousePosition;
   screenPos.z = 10;
  worldPos = Camera.main.ScreenToWorldPoint(screenPos);
  //transform.position = Vector3.Lerp(startPos, worldPos, 1);
 float timer = 0.01f;
 timer += Time.deltaTime;
transform.position = Vector3.Lerp(startPos, worldPos, timer);
 }
 }
}
//__________________________________
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
 [SerializeField] private GameObject[] enemyPrefabs;
 [SerializeField] private Transform LeftTop;
 [SerializeField] private Transform RightBottom;
  private float minX, maxX, minY, maxY;
  private void Start()
  {
   minX = LeftTop.position.x;
   maxX = RightBottom.position.x;
   minY = RightBottom.position.y;
   maxY = LeftTop.position.y;
   StartCoroutine(SpawnEnemy());
  }
private IEnumerator SpawnEnemy()
{
for (int i = 0; i < 30; i++)
{
  Vector2 position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
  GameObject enemy = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];
  Instantiate(enemy, position, Quaternion.identity, transform);
  yield return new WaitForSeconds(2.0f);
   }
  }
}
///////////////////////////////////////////////////////
//Playerにattach
//EventTriggerの設定
//LeftPos()
//RightPos()
//StopPos()
//JumpPos()
using UnityEngine;
public class PlayerController : MonoBehaviour
{
    private float speed;
    public Rigidbody2D rb;
    private float dirTurn;
 void Start()
  {
    //  rb = GetComponent();
      //  dirTurn = 1;
   }
 void Update()
   {
     transform.position += new Vector3(speed, 0, 0) * Time.deltaTime * 2;
  }
public void LeftPos()
    {
      speed = -1;
      transform.eulerAngles = new Vector3(0, 180, 0);
    }
 public void RightPos()
   {
      speed = 1;
      transform.eulerAngles = new Vector3(0, 0, 0);
    }
    public void JumpPos()
   {
     if (rb.velocity.y == 0)
    {
      rb.AddForce(new Vector2(0, 8), ForceMode2D.Impulse);
    //  Instantiate(bulletPrefab, transform.position, Quaternion.identity);
     }
}
  public void StopPos()
    {
      speed = 0;
     //   dirTurn = 0;
    }
}
//////////////////////////
using UnityEngine;
public class PlayerController : MonoBehaviour
{
 private float speed;
 public Rigidbody2D rb;
private float dirTurn;
// public Transform firePoint;
 public GameObject bulletPrefab;
 public GameObject bulletPrefab2;
 void Start()
    {
      //  dirTurn = 1;
    }
    void Update()
    {
      ransform.position += new Vector3(speed, 0, 0) * Time.deltaTime * 2;
    }
    public void LeftPos()
    {
     speed = -1;
    transform.eulerAngles = new Vector3(0, 180, 0);
    Instantiate(bulletPrefab2, transform.position, Quaternion.identity);
    }
    public void RightPos()
    {
    speed = 1;
    transform.eulerAngles = new Vector3(0, 0, 0);
    Instantiate(bulletPrefab, transform.position, Quaternion.identity);
    }
    public void JumpPos()
   {
   if (rb.velocity.y == 0)
     {
    rb.AddForce(new Vector2(0, 8), ForceMode2D.Impulse);
  // Instantiate(bulletPrefab, transform.position, Quaternion.identity);
        }
    }
    public void StopPos()
    {
        speed = 0;
     //   dirTurn = 0;
    }
}
//__________________
using UnityEngine;
public class Bullet : MonoBehaviour
{
  void Update()
 {
  transform.Translate(0.03f, 0, 0);
 if (transform.position.x > 10.0f)
 {
  Destroy(gameObject);
   }
    }
}
//__________________
using UnityEngine;
public class Bullet2 : MonoBehaviour
{
void Update()
    {
  transform.Translate(-0.03f, 0, 0);
   if (transform.position.x > 10.0f)
  {
    Destroy(gameObject);
 }
 }
}

月別
固定ページ