Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created July 2, 2017 13:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/18ad8e2d3ff8d051638f183b5275ef3b to your computer and use it in GitHub Desktop.
Save tsubaki/18ad8e2d3ff8d051638f183b5275ef3b to your computer and use it in GitHub Desktop.
マウスの位置を向く
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lookat : MonoBehaviour
{
Plane plane = new Plane();
float distance = 0;
void Update()
{
// カメラとマウスの位置を元にRayを準備
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// プレイヤーの高さにPlaneを更新して、カメラの情報を元に地面判定して距離を取得
plane.SetNormalAndPosition (Vector3.up, transform.localPosition);
if (plane.Raycast (ray, out distance)) {
// 距離を元に交点を算出して、交点の方を向く
var lookPoint = ray.GetPoint(distance);
transform.LookAt (lookPoint);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment