LineRender Not Detecting Collison
Hey, I need some help with LineRender code which has been giving me a lot of grief.
It just refuses to detect collision and I can't for the life of me fathom what exactly am I doing wrong here?
The layer mask is set correctly, all the objects have correct layers assigned, but it will detect collision in places that make no sense and it keeps refusing to detect it when it occurs, even if it does occur sometimes the endLine is drawn way before where the actual collision took place.
Here is the code if someone is able to help out (sorry for the mess, I've been trying everything I know):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[Header("Aim")]
[SerializeField] LayerMask groundMask;
[SerializeField] Camera mainCam;
[SerializeField] Transform aimedTransform;
[SerializeField] Transform prefabSpawn;
[SerializeField] Transform weaponTransform;
[SerializeField] float rotationSpeed = 5f;
[Header("Laser")]
[SerializeField] private LineRenderer laserRenderer;
[SerializeField] private LayerMask laserMask;
[SerializeField] private float laserLength;
//private bool isAiming = false;
void Start()
{
// Cursor.visible = false;
}
// void FixedUpdate()
// {
// Aim();
// if (Input.GetMouseButton(1))
// {
// RefreshLaser();
// } else
// {
// DestroyLaser();
// }
// }
void Update()
{
Aim();
if (Input.GetMouseButton(1))
{
RefreshLaser();
} else
{
DestroyLaser();
}
}
void Aim()
{
var (success, position) = GetMousePosition();
if (success)
{
// var direction = position - transform.position;
// var wepDirection = position - transform.position;
// float angle = Mathf.Atan2(direction.z, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.LookRotation(position - transform.position);
rotation = Quaternion.Euler(0, rotation.eulerAngles.y, 0);
//ignore height, look into this later
// direction.y = 0;
//Debug.Log(rotation.z);
// transform.forward = direction;
// rotate using Slerp
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
// weaponTransform.forward = wepDirection;
}
}
(bool success, Vector3 position) GetMousePosition()
{
var ray = mainCam.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out var hitInfo, Mathf.Infinity, groundMask))
{
//Raycast hit
return (success: true, position: hitInfo.point);
} else
{
//No hit
return (success: false, position: Vector3.zero);
}
}
private void RefreshLaser()
{
// if (laserRenderer == null)
// {
// return;
// }
laserRenderer.enabled = true;
Vector3 lineEnd;
Vector3 posit = prefabSpawn.position;
//use only z
posit.y = 0;
posit.x = 0;
if (Physics.Raycast(posit, aimedTransform.forward, out var hitinfo, laserLength, laserMask))
{
lineEnd = hitinfo.point;
Debug.Log("It hit something!");
}
else
{
lineEnd = posit + aimedTransform.forward * laserLength;
Debug.Log("It did not hit anything!");
}
laserRenderer.SetPosition(1, aimedTransform.InverseTransformPoint(lineEnd));
}
private void DestroyLaser()
{
laserRenderer.enabled = false;
}
}
Any help would be greatly appreciated!
Here's the picture of the strange behavior:
Your answer
![](https://koobas.hobune.stream/wayback/20220613053839im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity animator not playing animation, collider problems 0 Answers
Referencing a variable from another script 1 Answer
[SOLVED] Problem with OnCollisionEnter2D script after upgrading to Unity 5.3(from Unity 4.6) 1 Answer
create object on raycast collision that follows raycast. 0 Answers
Unity 5: AddForce Increases power when already being pushed towards a collider. How to make stop? 1 Answer