The laser problems
(google translate)
Tasks:
Make a beam of laser that is tied to the ship's weapon and moves with it. (Done)
Clicking the mouse creates a continuous beam at the cursor position. (Made partially, I have a problem with #4)
Upon contact with the collider, the creation of the beam stops at the point of the collider. (Made partially, I have a problem)
If the laser does not touch any collider, then the laser goes into the distance of the mouse position. (This is the main problem)
Problems:
(3) The beam ignores a collision with the collider if the mouse cursor is aimed at the object behind this collider. i.e. the beam passes through the collider.
(4) The ray is not created in an empty space.
How to direct a beam to the cursor point with a given length? Why does the ray pass through the object?
I'm new to programming. Teach me senpai.
void Start()
{
laser = GetComponent<LineRenderer>();
laser.material = laserMaterial;
laser.widthCurve = laserWidth;
laser.SetPositions(laserPos);
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
laser.SetPosition(0, transform.position);
if (Input.GetMouseButton(0))
{
laser.enabled = true;
if (Physics.Raycast(ray, out hit))
{
laser.SetPosition(1, hit.point + hit.normal); //The beam stops at the collider of the object.
}
else
{
laser.SetPosition(1, transform.forward * 3000); //It's an attempt to send a beam into space. Unsuccessful. A ray is created in a straight line.
}
}
else laser.enabled = false;
}
Your answer
Follow this Question
Related Questions
Menu object not responding 0 Answers
How can I display two elements of two list? 1 Answer
Scripting Help desperately needed. 1 Answer
Update() and FixedUpdate() not running on C# scripts 0 Answers