Created
December 24, 2016 23:35
-
-
Save KRNKRS/63478100f392106d057ff1c214ef1a1b 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 CameraFollow : MonoBehaviour { | |
public float moveSpeed = 3; | |
public float rotateSpeed = 3; | |
private GameObject lookTarget; | |
private GameObject cameraPositionTarget; | |
void Awake() | |
{ | |
lookTarget = GameObject.Find("CameraRotateOrigin").gameObject; | |
cameraPositionTarget = lookTarget.transform.FindChild("CameraPositionTarget").gameObject; | |
} | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void FixedUpdate () { | |
var nowPos = this.transform.position; | |
var targetPos = cameraPositionTarget.transform.position; | |
//変更箇所 ここから | |
RaycastHit hit; | |
var from = lookTarget.transform.position; | |
var dir = targetPos - from; | |
var dis = Vector3.Distance(targetPos, from); | |
if (Physics.Raycast(from, dir, out hit, dis, ~(1<<LayerMask.NameToLayer("Player")))) | |
{ | |
var avoidPos = hit.point - dir.normalized * 0.1f; | |
this.transform.position = avoidPos; | |
} | |
else | |
{ | |
this.transform.position = Vector3.Lerp(nowPos, targetPos, moveSpeed * Time.deltaTime); | |
} | |
//ここまで | |
var thisPos = this.transform.position; | |
var followTargetPos = lookTarget.transform.position; | |
var vectorToTarget = followTargetPos - thisPos; | |
var thisRotate = this.transform.rotation; | |
var targetRotate = Quaternion.LookRotation(vectorToTarget); | |
var newRotate = Quaternion.Lerp(thisRotate, targetRotate, rotateSpeed * Time.deltaTime).eulerAngles; | |
this.transform.eulerAngles = new Vector3( newRotate.x, newRotate.y, newRotate.z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment