Files
FebGameJam/Assets/Jacob Folder/ScriptRayHit.cs
T
Unknown 815bb4f6fa Iamdoa hsd;lqk ,sov nfk gk apms;k kjdod
I was in the wrong keyboard format ;(
2022-02-24 20:15:38 -06:00

81 lines
2.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptRayHit : MonoBehaviour
{
public RaycastHit2D hit;
public Ray ray;
private int counter;
public GameObject followCam;
public Camera staticCam;
private bool cam = false;
[Range(0.5f, 10f)] public float smoothSpeed = 1f;
[Range(1, 10)] public float rayLength = 4f;
private float angleStep = 4;
public Transform target;
public Rigidbody2D rb;
private Vector3 offset;
private LayerMask layerMask;
// Start is called before the first frame update
void Start()
{
counter = 0;
layerMask = LayerMask.GetMask("Background");
}
void FixedUpdate()
{
offset = Vector3.zero;
for (int i = 1; i <=angleStep; i++)
{
var rayAngle = Quaternion.AngleAxis((360f / angleStep) * i, target.forward) * target.up;
hit = Physics2D.Raycast(target.position, rayAngle, rayLength, layerMask);
if (hit)
{
offset += (rayAngle * rayLength) - (rayAngle * hit.distance);
}
//Vector3 smoothedPos = new Vector3((target.position.x - offset.x)*rayLength, target.position.y - offset.y, -1.3f);
Vector3 smoothedPos = Vector3.Lerp(transform.position, new Vector3((target.position.x - offset.x)*rayLength, target.position.y - offset.y, -100f), smoothSpeed * Time.fixedDeltaTime);
Debug.Log(smoothedPos);
followCam.transform.position = smoothedPos;
}
}
//Vector3 smoothedPos = Vector3.Lerp(transform.position, new Vector3((target.position.x - offset.x)*rayLength, target.position.y - offset.y, -1.3f), smoothSpeed * Time.fixedDeltaTime);
// Update is called once per frame
void Update()
{
/*hit = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.right), 4f);
if (hit)
{
Debug.Log(" Hit a wall "+ hit.collider.name);
Debug.Log(hit.distance);
//staticCam.enabled = true;
//followCam.enabled = false;
counter = 0;
//cam = true;
}
else if (cam && counter == 30)
{
staticCam.enabled = false;
followCam.enabled = true;
counter = 0;
cam = false;
Debug.Log("");
}
counter++;*/
}
}