LoginSignup
0
1

More than 3 years have passed since last update.

Unityでマウスの動きに合わせてGameObjectを動かす方法

Last updated at Posted at 2020-06-03

はじめに

こんな感じのものを作ります

movie1.gif

ステップ

  1. 動かしたいGameObjectを作成します
  2. 「MouseChase.cs」の名前でC#スクリプトを生成します
  3. 2のスクリプトを1のGameObjectにアタッチします

スクリプト

MouseChase.cs
using UnityEngine;

public class MouseChase : MonoBehaviour
{
    /// <summary>
    /// Update
    /// </summary>
    void Update()
    {
        transform.position = Input.mousePosition;
    }
}

補足

transform.position = Input.mousePosition;

上記の箇所ですが、以下のように書くこともできます

gameObject.transform.position = Input.mousePosition;

違いは参考のURLにて詳しく書いてますので、参考にしてみてください
上の記述のほうが早いようです

参考

gameObject.GetComponent() と transform の違い(または Unity における省略記法について)

0
1
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
0
1