オブジェクトの質量(mass)を増加させる
mass(質量)を変化させる
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
private float move_spped = 5;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
private bool isJumping = false;
void Start () {
// ★★
rb = GetComponent<Rigidbody>();
}
void Update () {
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0.0f, moveV);
rb.AddForce(movement * move_spped);
if(Input.GetButtonDown("Jump") && isJumping == false){
rb.velocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
void OnTriggerEnter(Collider other){
if(other.CompareTag("Coin")){
// ★★
// コインを1枚取得するたびごとに「質量」を10ずつ増加させる。
rb.mass += 10;
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, Camera.main.transform.position);
}
}
void OnCollisionEnter(Collision other){
if(other.gameObject.CompareTag("Floor")){
isJumping = false;
}
}
}
Unity Code Memo
他のコースを見るmass(質量)を変化させる
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
private float move_spped = 5;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
private bool isJumping = false;
void Start () {
// ★★
rb = GetComponent<Rigidbody>();
}
void Update () {
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0.0f, moveV);
rb.AddForce(movement * move_spped);
if(Input.GetButtonDown("Jump") && isJumping == false){
rb.velocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
void OnTriggerEnter(Collider other){
if(other.CompareTag("Coin")){
// ★★
// コインを1枚取得するたびごとに「質量」を10ずつ増加させる。
rb.mass += 10;
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, Camera.main.transform.position);
}
}
void OnCollisionEnter(Collision other){
if(other.gameObject.CompareTag("Floor")){
isJumping = false;
}
}
}
オブジェクトの質量(mass)を増加させる