Created
July 2, 2017 13:33
-
-
Save tsubaki/18ad8e2d3ff8d051638f183b5275ef3b to your computer and use it in GitHub Desktop.
マウスの位置を向く
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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