Skip to content

Instantly share code, notes, and snippets.

@Sammyjroberts
Last active October 17, 2016 05:39
Show Gist options
  • Save Sammyjroberts/ee8206cd6099989d47a507a5ef53a1ac to your computer and use it in GitHub Desktop.
Save Sammyjroberts/ee8206cd6099989d47a507a5ef53a1ac to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class PlayerFollow : MonoBehaviour
{
public Transform target;
public float smoothing = 5f;
Vector3 offset;
Camera camera;
LayerMask floorMask;
float camRayLength = 100f; // The length of the ray from the camera into the scene.
void Start ()
{
// Calculate the initial offset.
offset = new Vector3(0,10,-10);
gameObject.transform.eulerAngles = (new Vector3(45f,0f,0f));
camera = gameObject.GetComponent<Camera>();
floorMask = LayerMask.GetMask ("Floor");
}
void Update ()
{
if(target != null) {
Vector3 targetCamPos = target.position + offset;
//transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
// Create a ray from the mouse cursor on screen in the direction of the camera.
Ray camRay = camera.ScreenPointToRay (Input.mousePosition);
// Create a RaycastHit variable to store information about what was hit by the ray.
RaycastHit floorHit;
// Perform the raycast and if it hits something on the floor layer...
if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
{
// Create a vector from the player to the point on the floor the raycast from the mouse hit.
targetCamPos = ((floorHit.point+target.position)/2);
targetCamPos.y = 10;
Debug.Log(floorHit.point);
Debug.Log(target.position);
Debug.Log(targetCamPos);
}
transform.position = Vector3.Lerp (transform.position, (targetCamPos) + offset, smoothing * Time.deltaTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment