[주사위 던지기용 소스]


* Terrain 태그 = Ground, 큐브가 Ground 에 닿아있을 때만 클릭으로 큐브를 던질 수 있음

* 큐브 inspector 에서 position x,z 값 고정하여 다른 방향으로 움직이지 않고 오직 위아래로만 움직이게끔 만듬



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

public class cubeMove : MonoBehaviour {

public int speed = 10;
public bool isJumpping = false;

private Rigidbody rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
    }
    
    // Update is called once per frame
    void Update () {
moveCube();
}

void moveCube()
{
if (!isJumpping)
{
if (Input.GetMouseButtonDown(0))
{
this.isJumpping = true;
transform.position = new Vector3(50,1,50);
transform.rotation = Quaternion.identity;
rb.AddForce(transform.up * 500);
rb.AddTorque(Random.Range(0,500), Random.Range(0, 500), Random.Range(0, 500));
this.GetComponent<Rigidbody>().velocity = Vector3.up * this.speed;
}
}
}


private void OnCollisionEnter(Collision collision)
{
if (collision.transform.tag == "Ground")
{
this.isJumpping = false;
}
}

}





+ Recent posts