TheKingsRace/Assets/Scripts/Blink.cs
Evan Nydahl 981b42dc51 Added a rough kicking prototype
-two scritpts that deal with kick logic
-added ball and leg to player in Evan_Prototypes scene
2021-10-08 13:52:36 -05:00

96 lines
2.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Blink : MonoBehaviour{
// Start is called before the first frame update
private LineRenderer beam;
private Camera cam;
public CharacterController controller;
private Vector3 origin;
private Vector3 endPoint;
private Vector3 mousePos;
private RaycastHit hit;
private void Awake()
{
controller = GetComponent<CharacterController>();
}
void Start(){
// Grabbed our laser.
beam = this.gameObject.AddComponent<LineRenderer>();
beam.startWidth = 0.2f;
beam.endWidth = 0.2f;
// Grab the main camera.
cam = Camera.main;
}
private void Update(){
if (Input.GetMouseButton(1)){
// Finding the origin and end point of laser.
origin = this.transform.position + this.transform.forward * this.transform.lossyScale.z;
// Finding mouse pos in 3D space.
mousePos = Input.mousePosition;
mousePos.z = 20f;
endPoint = cam.ScreenToWorldPoint(mousePos);
// Find direction of beam.
Vector3 dir = endPoint - origin;
dir.Normalize();
// Are we hitting any colliders?
if (Physics.Raycast(origin, dir, out hit, 20f)){
// If yes, then set endpoint to hit-point.
endPoint = hit.point;
}
// Set end point of laser.
beam.SetPosition(0, origin);
beam.SetPosition(1, endPoint);
// Draw the laser!
beam.enabled = true;
/*Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit raycastHit;
if (Physics.Raycast(ray, out raycastHit, 5.0f)){
LineRenderer.SetPosition(1, raycastHit.point);
}*/
}
if (Input.GetMouseButtonUp(1)){
beam.enabled = false;
//disable character controller for a brief second for teleportation
//this.gameObject.GetComponent<CharacterController>().enabled = false;
//get
Vector3 bump = new Vector3(0, .5f, 0);
//if teleporting due to hit to object, bump them a bit outside normal
if(hit.point != null) {
transform.position = endPoint + hit.normal * 1.25f;
}
//if teleporting in the air or something, just spawn at endpoint
else
{
transform.position = endPoint;
}
//reenable character controller
//this.gameObject.GetComponent<CharacterController>().enabled = true;
}
}
// Update is called once per frame
void FixedUpdate()
{
}
}