ゴーストの作成(揺れながら近づいてくる)


揺れながら近づいてくる
using System;
using UnityEngine;
public class Ghost : MonoBehaviour
{
private GameObject target;
private float moveSpeed = 1.0f; // 移動速度
private float floatingHeight = 0.5f; // 上下の揺れ幅
private float floatingSpeed = 1.5f; // 揺れの速さ
void Start()
{
target = GameObject.Find("Player");
}
void Update()
{
if(!target)
{
return;
}
// ターゲットへの向きの計算
Vector3 direction = (target.transform.position - transform.position).normalized;
// 移動
transform.position += direction * moveSpeed * Time.deltaTime;
// 浮遊感(サイン波)
float hover = MathF.Sin(Time.time * floatingSpeed) * floatingHeight;
transform.position += Vector3.up * hover * Time.deltaTime;
}
}
【Unity6版】VR_Dungeon(全18回)
他のコースを見る

揺れながら近づいてくる
using System;
using UnityEngine;
public class Ghost : MonoBehaviour
{
private GameObject target;
private float moveSpeed = 1.0f; // 移動速度
private float floatingHeight = 0.5f; // 上下の揺れ幅
private float floatingSpeed = 1.5f; // 揺れの速さ
void Start()
{
target = GameObject.Find("Player");
}
void Update()
{
if(!target)
{
return;
}
// ターゲットへの向きの計算
Vector3 direction = (target.transform.position - transform.position).normalized;
// 移動
transform.position += direction * moveSpeed * Time.deltaTime;
// 浮遊感(サイン波)
float hover = MathF.Sin(Time.time * floatingSpeed) * floatingHeight;
transform.position += Vector3.up * hover * Time.deltaTime;
}
}
ゴーストの作成(揺れながら近づいてくる)