27
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Unity] 特定のオブジェクトだけ重力を変える

Posted at

特定のオブジェクトのみ重力を変更したいと思うことがあります。
兄は普通のジャンプでいいけど、弟はふわふわしたジャンプにしたい時などですね。

重力を変更したいオブジェクトに下記スクリプトを追加して、パラメータlocalGravityに加速度を設定するだけで重力を変更することが可能です。

##スクリプト

gravityController.cs
using UnityEngine;
using System.Collections;

public class gravityController : MonoBehaviour {

    public Vector3 localGravity;

    private Rigidbody rb;

    void Start () {
        rb = this.GetComponent<Rigidbody>();
        rb.useGravity = false;
    }

    void FixedUpdate () {
        setLocalGravity ();
    }

    void setLocalGravity(){
        rb.AddForce (localGravity, ForceMode.Acceleration);
    }

}
27
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?