0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Kling AI × Claude Codeで、マウスを追いかけてくるアバターを作った話【プロンプト全公開】

0
Last updated at Posted at 2026-12-01

はじめに

ポートフォリオサイトに、マウスを追いかけてくる自分のアバターを住まわせました。

マウスを追いかけてくるアバターがいるポートフォリオのトップページ

カーソルを動かすと、アバターが視線と顔の向きでちゃんと追いかけてきます。実物はこちらで動いています(PCでカーソルを動かしてみてください):

作り方:

  1. ChatGPTで自分のアバター画像を作る(画像生成はお好きなツールでOK)
  2. Kling AIで「視線の方向別」の短い動画を9本生成する
  3. Claude Codeに、動画をコマ画像にしてマウス位置で向きを切り替えるNext.jsコンポーネントを作らせる

かなりの数のプロンプトと設定を試したので、この記事では実際に採用した9本のプロンプトを原文のまま全公開します。失敗から学んだコツも全部書きます。

今回は自分のアバターを使いましたが、仕組みはキャラクターを選びません。可愛いオリジナルキャラクターでも、同じ手順で好きなように動かせます。 マスター画像を差し替えるだけです。

全体の仕組み

発想はシンプルです。「正面→ある方向を向ききる」までの動画を方向ごとに作り、コマ画像に分解して、マウスの位置に合わせてコマを出し分けるだけ。

  • どっちを向くか = アバターから見たマウスの角度で決める
  • どれくらい向くか = アバターからマウスまでの距離で決める(遠いほど大きく振り向く)
  • マウスが画面の下側にあるときは、手元のPCで作業に戻る

リアルタイムに顔を動かしているわけではなく、用意したコマを選んでいるだけなので実装は軽量。でもマウスを近づけたり遠ざけたりすると首の振り幅まで変わるので、見た目には「ちゃんと追ってくる」ように見えます。

STEP1: ベースになるアバター画像を作る

まず、すべての元になるマスター画像を1枚作ります。私はChatGPTで、Pixar風3Dの自分アバター(ピンクのヘッドホン+MacBook)を作りました。

ここのプロンプトはあえて載せません。自分の写真をAIに送って、どんなアバターにしたいかは自分で決めてください。 画風、服装、小物(私の場合はヘッドホンとMacBook)を選ぶところが、いちばん「自分らしさ」が出る部分だからです。

ポイントは1枚に決めること。以降の動画は全部この1枚を起点に生成するので、ここで迷いを断ち切ります。服装・背景・ライティングがシンプルなほうが、あとの動画生成が安定します。

私の失敗:背景は最初からグリーンスクリーンにすべきでした。
私はベース画像を普通の背景(ベージュピンク)で作ってしまい、あとから動画の背景を抜くのに苦労しました。ベース画像の時点で背景をグリーンスクリーン(単色の緑)にしておけば、動画生成後にクロマキーで背景を透過して、サイトのどんな背景にも自然に馴染ませられます。これから作る人は、ベース画像のプロンプトに solid green screen background を入れておいてください。

STEP2: Kling AIで「視線の方向別」動画を9本生成する

まずは画面を3×3に分けるイメージで、9方向ぶん作りました。構成はこうです:

左 中央 右
上 ↖ Top Left ↑ Top ↗ Top Right
中央 ← Left ● Main(待機) → Right
下 ↙ Left2 💻 PC(作業中) ↘ Right2

下段の中央は「下を見る」ではなく、手元のPCに視線を落としてタイピングする動画にしました。マウスが画面の下にいるあいだ、アバターは自分の作業に戻っている——この小ネタだけでキャラに生活感が出ます。長さは3〜10秒でバラバラでOKです。

実際に生成した9本はこちら(左下の数字が動画の長さ):

9方向の視線動画の一覧

🔑 最重要ポイント:First FrameとLast Frameに「同じマスター画像」を入れる

いろいろなプロンプトと設定を試しましたが、成功の9割はこれでした。

Kling 4.0のKeyframes(キーフレーム)モードで、First FrameとLast Frameの両方に同じマスター画像を指定します。

Kling 4.0のKeyframesモードでFirst/Last Frameに同じ画像を指定した画面

  • 9本すべての動画が、同じ絵で始まり、同じ絵で終わる
  • つまり、どの動画からどの動画に切り替えても絵が飛ばない(全部の接続点が同じフレームだから)
  • 動画の途中でキャラが多少動いても、最後は必ずマスター画像に戻ってくるので、キャラ崩れが構造的に起きにくい

これを指定せずにプロンプトだけで頑張ると、動画ごとに顔が微妙に別人になったり、切り替えの瞬間にガクッと飛んだりします。プロンプトを磨くより先に、キーフレームで両端を固定する——これが結論です。

生成設定: Kling 4.0 / Keyframesモード / 1080p / 3〜10s(長さは方向ごとに設定。上の一覧画像の数字のとおり)

試行錯誤から学んだプロンプトのコツ

9本を作る過程で効いたこと・ハマったことです。

1. 視線の方向は「ゾーン名」ではなく「アバターから見たマウスの方角」で書く

アバターはページの下の方にいます。だから、画面中段のゾーン用の動画でも、アバターから見ればマウスは斜め上。実際、私のLeft/Right用プロンプトの本文は「upper-left / upper-right」です。下段の左右は、アバターからはほぼ真横なので「screen-left / screen-right」。ゾーン名につられて視線を作ると、アバターの位置と合わなくて嘘くさくなります。

上段3本の視線イメージ:アバター(●)からマウスのいる方向を見る

2. 左右の言葉指定は、あてにならない

これが一番の罠でした。実は私の最終プロンプト、TopLeft用もTopRight用も本文は「upper-left」です。左右を言葉で指定しても出力が反対を向くことがあるため、同じ系統のプロンプトで複数回生成して、出力の向きを見てファイルを振り分けました。左右で悩んだら、プロンプトを磨くより数を出すほうが早いです。

3. 動きは「数値+強制+軌道」で縛る

  • 角度を数値で:「by approximately 25 degrees」(「ちょっと上」では動きが小さすぎる)
  • 動きの強制:「must be clearly visible」
  • 軌道まで指定:「simultaneously, not sequentially(同時に動け、順番に動くな)」「Do not look straight up first(先に真上を見るな)」——方向だけ指定すると、変な経路で動きます

4. Negative Prompt欄を使い分ける

禁止事項は本文に書くより、KlingのNegative Prompt欄に入れるほうが効きました。私は2セットを使い分けています:

  • 動き抑制セット(上段・待機用): Head movement, head rotation, head tilting, nodding, body swaying, shoulder movement, gaze shifting, facial expression changes, open mouth, visible teeth, talking, exaggerated breathing, camera movement, zoom, cropping, color shifts, lighting changes, character redesign, music, sound.
  • 背景抑制セット(背景が動く失敗が出た方向用): Moving background, background animation, moving objects, objects passing behind the character, additional characters, particles, floating objects, shadows moving across the background, flickering, visual effects, camera movement, zoom, cropping, color shifts, character redesign, visible teeth, open mouth, music.

方向ごとに出る問題が違うので、失敗を見てからネガティブを差し替えるのが現実的です。

実際に使ったプロンプト9本(原文そのまま)

クリックで開いてコピーしてください。すべてKeyframesモードで First/Last Frame に同じマスター画像を入れた状態で使います。キャラが女性でない場合は She/Her を書き換えを。

↖ Top Left(8s)
She lifts her chin and turns her head diagonally toward the upper-left corner of the image. Her face and eyes clearly look upward-left. She holds this pose until the video ends, with a soft, natural smile.
Preserve the original 3D character design, colors, lighting, and composition. Fixed camera, static background, no audio.

Negative Prompt:
Returning to center, looking right, looking around, repeated movements, visible teeth, camera movement, color changes.
↑ Top(5s)
Create a single 3-second animation using the uploaded master base image as the exact starting frame.
She slowly lifts her chin and tilts her head upward by approximately 15 degrees. Her eyes clearly look upward toward the top center of the screen, above the camera.
The upward head movement must be clearly visible. She must not look sideways.
She maintains a lively, curious expression with a gentle closed-mouth smile. No visible teeth.
She holds the upward gaze until the end without returning to the center.
Keep her shoulders, arms, body, and laptop stationary. Preserve the exact original Pixar-inspired 3D character design, facial features, proportions, pastel colors, lighting, and background.
Fixed camera, single continuous shot. No zoom, no cropping, no background movement, no new objects, no audio.

Negative Prompt:
Head movement, head rotation, head tilting, nodding, body swaying, shoulder movement, gaze shifting, facial expression changes, open mouth, visible teeth, talking, exaggerated breathing, camera movement, zoom, cropping, color shifts, lighting changes, character redesign, music, sound.
↗ Top Right(5s)※本文はupper-left — 出力の向きで採用
Create a single 5-second animation starting from the exact master base image.
She looks at a point high above the camera, toward the upper-left corner of the screen.
In one coordinated diagonal movement, she visibly raises her chin, tilts her entire head backward by approximately 25 degrees, and rotates her face toward screen-left by approximately 15 degrees.
The upward tilt of her head must be clearly visible. Her face and nose point diagonally upward-left, and her eyes look high above eye level.
Her final pose must clearly show that she is looking UP AND LEFT, not simply looking left.
She maintains a lively, curious expression with a gentle closed-mouth smile. Hold this final pose until the end.
Preserve the exact original Pixar-inspired 3D character design, facial features, proportions, colors, lighting, and framing.
Keep her body, shoulders, arms, laptop, and background completely stationary.
Fixed camera, single continuous shot, no zoom, no new objects, no audio.

Negative Prompt:
Head movement, head rotation, head tilting, nodding, body swaying, shoulder movement, gaze shifting, facial expression changes, open mouth, visible teeth, talking, exaggerated breathing, camera movement, zoom, cropping, color shifts, lighting changes, character redesign, music, sound.

※「左右の言葉指定はあてにならない」の実例がこれです。upper-leftと書いたプロンプトから複数生成し、右上を向いた出力をTop Right用に採用しました。

← Left(3s)— アバターから見ると斜め上
Create a single 3-second animation using the uploaded master base image as the exact starting frame.
She gently shifts her eyes toward the upper-left corner of the screen, followed by a slight upward and leftward head movement. Her movement is smooth, natural, and controlled.
She maintains a lively, mildly curious expression with a soft closed-mouth smile. No visible teeth.
She holds her target gaze until the end without returning to the center.
Only her eyes and head are allowed to move. Her shoulders, arms, body, and laptop remain completely stationary.
The background remains completely static, empty, and identical to the original image.
Preserve the exact original Pixar-inspired 3D character design, facial features, proportions, pastel colors, brightness, saturation, lighting, and camera framing.
Fixed camera. Single continuous shot. No zoom, no cropping, no new objects, no background movement, no visual effects, no audio.

Negative Prompt:
Head movement, head rotation, head tilting, nodding, body swaying, shoulder movement, gaze shifting, facial expression changes, open mouth, visible teeth, talking, exaggerated breathing, camera movement, zoom, cropping, color shifts, lighting changes, character redesign, music, sound.
● Main/待機(5s)— 生きた静止画
Create a single 5-second animation using the uploaded master base image as the exact starting frame.
She looks directly at the viewer with bright, lively eyes and a gentle, natural closed-mouth smile.
Her head remains perfectly upright, centered, and completely stationary throughout the entire video. Her gaze remains fixed directly at the camera.
The only movement is extremely subtle, natural breathing. Keep the movement minimal, almost like a living still image.
Maintain the exact same facial expression and smile throughout the animation.
Preserve the original Pixar-inspired 3D character design, facial features, proportions, hairstyle, pink headphones, white shirt, and laptop.
Maintain identical colors, brightness, saturation, skin tone, lighting, background, and camera framing in every frame.
Both shoulders, arms, elbows, and the entire laptop remain fully visible.
The first and last frames must match for a seamless loop.
Completely fixed camera. No head movement, no head tilting, no head rotation, no eye movement, no body swaying, no lip movement, no visible teeth, no zoom, no cropping, no cuts, no audio.

Negative Prompt:
Head movement, head rotation, head tilting, nodding, body swaying, shoulder movement, gaze shifting, facial expression changes, open mouth, visible teeth, talking, exaggerated breathing, camera movement, zoom, cropping, color shifts, lighting changes, character redesign, music, sound.

ループ再生するので、プロンプト側にも「The first and last frames must match for a seamless loop」を入れて、キーフレーム固定と二重で効かせています。

→ Right(5s)— 軌道まで指定した版
Create a single 5-second animation using the uploaded master base image as the exact starting frame.
She moves her eyes and head directly toward the upper-right corner of the screen in one smooth diagonal motion.
Her chin lifts and her head turns toward screen-right simultaneously, not sequentially. Her eyes immediately follow the same diagonal direction.
Do not look straight up first. Do not look horizontally to the right afterward. Move directly from the center to the upper-right corner in one continuous movement.
She maintains a lively, curious expression with a gentle closed-mouth smile. No visible teeth.
Hold the final upper-right gaze until the end without changing direction or returning to the center.
Preserve the exact original Pixar-inspired 3D character design, facial features, proportions, colors, brightness, lighting, and composition.
Keep her shoulders, arms, body, laptop, and background completely stationary.
Fixed camera, single continuous shot. No cuts, no zoom, no cropping, no additional objects, no background movement, no audio.

Negative Prompt:
Head movement, head rotation, head tilting, nodding, body swaying, shoulder movement, gaze shifting, facial expression changes, open mouth, visible teeth, talking, exaggerated breathing, camera movement, zoom, cropping, color shifts, lighting changes, character redesign, music, sound.
↙ Left2/左下(3s)— アバターから見るとほぼ真横
Create a single 3-second animation using the uploaded master base image as the exact starting frame.
She gently shifts her eyes toward screen-left, followed by a subtle head turn. Her movement is smooth, natural, and controlled.
She maintains a lively, mildly curious expression with a soft closed-mouth smile. No visible teeth.
She holds her left-facing gaze until the end without returning to the center.
Only her eyes and head are allowed to move. Her shoulders, arms, body, and laptop remain completely stationary.
The background must remain completely static, empty, and identical to the original image throughout the entire video.
Preserve the exact original Pixar-inspired 3D character design, facial features, proportions, pastel colors, brightness, saturation, lighting, and camera framing.
Fixed camera. Single continuous shot. No zoom, no cropping, no new objects, no background movement, no visual effects, no audio.

Negative Prompt:
Moving background, background animation, moving objects, objects passing behind the character, additional characters, particles, floating objects, shadows moving across the background, flickering, visual effects, camera movement, zoom, cropping, color shifts, character redesign, visible teeth, open mouth, music.

この方向では背景が動く失敗が出たので、Negative Promptを背景抑制セットに差し替えています。

💻 PC/作業中(10s)
Create a single 5-second animation starting from the exact master base image.
She gently lowers her chin and shifts her eyes downward toward the center of her laptop screen.
She begins typing naturally on the keyboard, fully concentrating on her work. Her eyes subtly scan the laptop screen, and she blinks naturally while breathing softly.
She maintains a calm, focused, and lively expression with a subtle closed-mouth smile, as if she is enjoying her creative work.
Once she looks down at the laptop, she maintains this working position until the end. She never looks back at the camera or sideways.
Keep her hands hidden behind the laptop. Only very subtle arm movements associated with typing are allowed.
Preserve the exact original Pixar-inspired 3D character design, facial features, proportions, hairstyle, pastel colors, brightness, saturation, lighting, and framing.
Keep the laptop, desk, and background completely stationary.
Fixed camera, single continuous shot, no zoom, no cropping, no new objects.
Only soft, natural keyboard typing sounds. No background music, no voice, no dialogue.

Negative Prompt:
Looking at the camera, looking sideways, looking upward, excessive head movement, head swaying, nodding, exaggerated typing, visible hands, visible teeth, open mouth, talking, sad expression, tired eyes, exaggerated facial expressions, body swaying, camera movement, zoom, cropping, color shifts, lighting changes, character redesign, moving background, additional objects, background music, voice.

工夫ポイント:手はラップトップの裏に隠す(AIは手の生成が崩れやすいので、最初から見せない)。Negative Promptの「sad expression, tired eyes」は、疲れた顔で仕事をさせないため。

↘ Right2/右下(3s)— アバターから見るとほぼ真横
Create a single 3-second animation using the uploaded master base image as the exact starting frame.
She gently shifts her eyes toward screen-right, followed by a subtle head turn. Her movement is smooth, natural, and controlled.
She maintains a lively, mildly curious expression with a soft closed-mouth smile. No visible teeth.
She holds her right-facing gaze until the end without returning to the center.
Only her eyes and head are allowed to move. Her shoulders, arms, body, and laptop remain completely stationary.
The background remains completely static, empty, and identical to the original image.
Preserve the exact original Pixar-inspired 3D character design, facial features, proportions, pastel colors, brightness, saturation, lighting, and camera framing.
Fixed camera. Single continuous shot. No zoom, no cropping, no new objects, no background movement, no visual effects, no audio.

Negative Prompt:
Moving background, background animation, moving objects, objects passing behind the character, additional characters, particles, floating objects, shadows moving across the background, flickering, visual effects, camera movement, zoom, cropping, color shifts, character redesign, visible teeth, open mouth, music.

STEP3: Claude Codeにコマ画像化と実装を任せる

動画ができたら、あとはClaude Codeにお願いしました。頼み方はたとえばこんな感じです:

Kling で作ったアバターの動画を、ポートフォリオの Hero に置きたい。

- 各方向の動画を、背景を抜いた透過WebPのコマ画像に分解して public/avatar/ に置いて
- マウスの角度で向く方向を、アバターからの距離で振り向く大きさ(どのコマか)を決めて Canvas に描画
- マウスが画面の下側にあるときと、5秒操作がないときは PC 作業中の動画をループ
- 方向が変わるときは、いったん正面に戻してから切り替える
- タッチ端末はマウス追従なし。画像は軽い解像度に

動画のコマ画像化と背景の透過も、Claude Codeにやってもらいました。最終的にサイトで使っているのは、視線6方向(右・右上・上・左上・左上と左の間・左)×各20コマと、作業中ループ、正面の1枚です。PCでは1080px、タッチ端末では540pxの画像を読み込みます。

できあがったコンポーネントの核の部分はこんな形です(実物から抜粋して簡略化):

// 画面上の角度(右=0°、反時計回り)
const DIRS: Record<string, number> = {
  right: 0, "top-right": 45, top: 90, "top-left": 135, "left-high": 157.5, left: 180,
};

// マウスの角度に一番近い方向を選ぶ。下側なら "bottom"(作業に戻る)
const pickDir = (nx: number, ny: number): string => {
  let ang = (Math.atan2(-ny, nx) * 180) / Math.PI;
  if (ang < 0) ang += 360;
  if (ang > 202.5 && ang < 337.5) return "bottom";
  let best = "right", bd = 1e9;
  for (const d of Object.keys(DIRS)) {
    let dd = Math.abs(DIRS[d] - ang);
    if (dd > 180) dd = 360 - dd;
    if (dd < bd) { bd = dd; best = d; }
  }
  return best;
};

const onMove = (e: MouseEvent) => {
  const r = el.getBoundingClientRect();
  const nx = (e.clientX - (r.left + r.width / 2)) / (window.innerWidth / 2);
  const ny = (e.clientY - (r.top + r.height / 2)) / (window.innerHeight / 2);
  const mag = Math.min(1, Math.hypot(nx, ny) * 1.15); // 遠いほど大きく振り向く
  const d = pickDir(nx, ny);
  if (d === "bottom") { setMode("idle"); return; }    // 下側なら作業に戻る
  if (mode === "idle") setMode("follow");
  if (mag < 0.12) { tgtMag = 0; return; }
  // 方向が変わるときは、いったん正面まで戻してから切り替える
  if (d !== curDir && curMag > 0.12) { pendingDir = d; pendingMag = mag; tgtMag = 0; }
  else { tgtDir = d; pendingDir = null; tgtMag = mag; }
};

// 毎フレーム(requestAnimationFrame):振り向く大きさをなめらかに追いかけて、そのコマを描く
curMag += (tgtMag - curMag) * 0.22;
if (pendingDir && curMag < 0.1) { curDir = pendingDir; tgtDir = pendingDir; pendingDir = null; tgtMag = pendingMag; }
if (!pendingDir) curDir = tgtDir;
const frames = dirs[curDir];                         // その方向の20コマ
const idx = Math.round(curMag * (frames.length - 1));
draw(idx <= 0 ? center : frames[idx]);               // 0なら正面の1枚

実装側のポイント:

  • 動画ではなくコマ画像をCanvasに描く:マウスの距離に合わせて「どのコマを出すか」を自由に選べるので、首の振り幅が連続的に変わります
  • 方向を変えるときは正面を経由する:どの方向の動画も最初のフレームがマスター画像(正面)なので、正面を通れば絵が飛びません。キーフレーム固定の効果は、実装側にも効いています
  • 切り替えは220msのクロスフェード:前のコマの上に新しいコマを重ねて描くので、途中で全体が透けません
  • 全コマを読み込むまでは静止画を表示:読み込み中にアバターが消えたり、カクついたりしないように
  • タッチ端末と「視差効果を減らす」設定では追従しない:タッチ端末は作業中ループだけ、視差効果を減らす設定では静止画にしています

ちょっとした遊びも足しています。アバターをクリックすると手を振って、チャット風の吹き出しで話しかけてきます。サウンドをオンにしていると、キーボードを打っているコマの間だけタイピング音が鳴ります。

ハマりどころ(試行錯誤の総まとめ)

  • 最大の学び:プロンプトより先にキーフレーム — 両端のフレームを固定しない限り、動画ごとに顔が変わる・切り替えで絵が飛ぶ問題は解決しませんでした
  • 左右の言葉指定はあてにならない — upper-leftと書いても右を向くことがある。複数生成して出力で振り分けるのが結局一番早い
  • 視線は「アバターから見た方角」で設計する — ゾーン名(画面上の位置)につられると、アバターの位置と視線が合わず嘘くさくなる
  • Negative Promptは方向ごとに育てる — 出る失敗が方向ごとに違う(背景が動く、キョロキョロする…)ので、失敗を見てからネガティブを差し替える
  • 背景はグリーンスクリーンで作るべきだった — 普通の背景で作ると、あとから背景を抜くのにかなり手間がかかります。最初から solid green screen background で作るのが正解でした
  • コマ画像は地味に重い — 方向ごとのコマは20枚あれば十分なめらかでした。タッチ端末には半分の解像度の画像を出し分けています

まとめ

  • 仕組みは「マウスの角度で方向、距離でコマを選ぶ」だけ。リアルタイムのトラッキング不要で“追ってくる感”が出せる
  • 成功の鍵は KlingのKeyframesモードで First/Last Frame に同じマスター画像を入れること
  • 左右はプロンプトで制御しきれない。数を出して、出力で振り分ける
  • 画像生成はChatGPTでなくても、手持ちの好きなツールでOK
  • 自分のアバターでなくても、オリジナルキャラクターでも同じ手順で動かせる
  • コマ画像化も実装もClaude Codeに任せて、こだわり(正面経由の切り替え、作業に戻る条件、タッチ対応)だけ指示する

ポートフォリオの第一印象がかなり変わるので、ぜひ自分のアバターを住まわせてみてください。実物はこちらで動いています:

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?