LoginSignup
26
30

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);
    }

}
26
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
26
30