投げる機能の実装
投げる機能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThrowItem : MonoBehaviour
{
public GameObject[] items;
public AudioClip throwSound;
public AudioClip changeSound;
public float throwSpeed;
private int num;
void Update()
{
Throw();
ChangeItem();
}
void Throw()
{
if (Input.GetKeyDown(KeyCode.Z))
{
GameObject item = Instantiate(items[num], transform.position, Quaternion.identity);
Rigidbody itemRb = item.GetComponent<Rigidbody>();
itemRb.AddForce(transform.forward * throwSpeed);
AudioSource.PlayClipAtPoint(throwSound, transform.position);
}
}
void ChangeItem()
{
if(Input.GetKeyDown(KeyCode.X))
{
AudioSource.PlayClipAtPoint(changeSound, transform.position);
num = (num + 1) % items.Length;
}
}
}
【2019版】X_Mission(基礎/全51回)
他のコースを見る投げる機能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThrowItem : MonoBehaviour
{
public GameObject[] items;
public AudioClip throwSound;
public AudioClip changeSound;
public float throwSpeed;
private int num;
void Update()
{
Throw();
ChangeItem();
}
void Throw()
{
if (Input.GetKeyDown(KeyCode.Z))
{
GameObject item = Instantiate(items[num], transform.position, Quaternion.identity);
Rigidbody itemRb = item.GetComponent<Rigidbody>();
itemRb.AddForce(transform.forward * throwSpeed);
AudioSource.PlayClipAtPoint(throwSound, transform.position);
}
}
void ChangeItem()
{
if(Input.GetKeyDown(KeyCode.X))
{
AudioSource.PlayClipAtPoint(changeSound, transform.position);
num = (num + 1) % items.Length;
}
}
}
投げる機能の実装