Hello, I am new to unity and pretty much new to programming. Anyways…I am trying to use raycast2Ds for reference points for a laser. When I use hit.distance and shoot it in a vertical direction…the distance becomes ‘reversed’ as is the closer I get the larger the hit.distance returns. When I shoot it in a horizontal direction the distance returned is ‘normal’. I have this script attached to a child object(which is set somewhat away from the avatar to avoid contact) of another object that rotates towards the mouse position which is also a child of the actual avatar. Here is the laser script. (Very sloppy I’m sure, just testing for now)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LaserScript : MonoBehaviour {
//ray from player
private Vector3 reflect;
private Vector3 incom;
private Vector2 reflect2D;
private Vector2 hitPoint;
//ray for 1st bounce
private Vector3 reflect2;
private Vector3 incom2;
private Vector3 reflect2D2;
private Vector2 hitPoint2;
//ray for 2nd bounce
private Vector3 reflect3;
private Vector3 incom3;
private Vector3 reflect2D3;
private Vector2 hitPoint3;
public int laserLengthDefault; // laser lenght
//temp lenghts for mesh
private int laserLength1;
private int laserLength2;
private int laserLength3;
//for mesh creatoon
public List<Vector3> newVertices = new List<Vector3>();
public List<int> newTriangles = new List<int>();
public List<Vector2> newUV = new List<Vector2>();
private Mesh mesh;
//mesh texture
private float tUnit = 0.25f;
private Vector2 text = new Vector2 (0,0);
//mesh starting coords
public float x;
public float y;
public float z;
// Use this for initialization
void Start ()
{
laserLength1 = laserLengthDefault;
laserLength2 = laserLengthDefault;
laserLength3 = laserLengthDefault;
//
//sets up initial mesh
//
/*
*
* ALL MESH FUNCTIONS DISABLED FOR NOW
*
*
mesh = GetComponent<MeshFilter>().mesh;
x = transform.position.x;
y = transform.position.y + .3f;
z = transform.position.z + 0.1f;
newVertices.Add( new Vector3(x, y - 1, z));
newVertices.Add( new Vector3(x, y - 1, z - 0.2f));
newVertices.Add( new Vector3(x, y, z - 0.2f));
newVertices.Add( new Vector3(x, y, z));
newTriangles.Add(0);
newTriangles.Add(1);
newTriangles.Add(3);
newTriangles.Add(1);
newTriangles.Add(2);
newTriangles.Add(3);
newUV.Add(new Vector2(tUnit * text.x, tUnit * text.y + tUnit));
newUV.Add(new Vector2(tUnit * text.x + tUnit, tUnit * text.y + tUnit));
newUV.Add(new Vector2(tUnit * text.x + tUnit, tUnit * text.y));
newUV.Add(new Vector2(tUnit * text.x, tUnit * text.y));
*/
}
// Update is called once per frame
void Update ()
{
//calls ray functions
CastRay();
FormLaser();
//creates mesh on fly
/*
mesh.Clear();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.uv = newUV.ToArray();
mesh.Optimize();
mesh.RecalculateNormals();
*/
}
public void CastRay()
{
//Cast Ray from player at set length
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.rotation * Vector2.up, laserLengthDefault);
//draw ray from player for ref
Debug.DrawRay(transform.position, transform.rotation * Vector2.up, Color.white);
//if ray from player hits
if (hit)
{
Debug.Log (hit.distance);
//if distance is zero (far)
//sets laser length to 0 otherwise lenght will be 3 because
//distance is larger the closer I get to hit point..
//VERY ODD
//
//
if (hit.distance <= 0f)
{
laserLength1 = 0;
}
else
{
laserLength1 = laserLengthDefault;
}
//sets world cords of hit point
reflect.x = hit.normal.x;
reflect.y = hit.normal.y;
//gets angle of hit point
incom = Vector3.Reflect(transform.rotation * Vector2.up, reflect);
//for reference cast?
//sets angle of hit for next cast
//may not be needed?
//maybe needed so z is automaticlly 0
reflect2D.x = incom.x;
reflect2D.y = incom.y;
//sets hit point away from collider so next ray doesnt immediatly hit
hitPoint = hit.point + hit.normal*0.1f;
//adjusts mesh vertices based on hit
//works ok when verticle
//not so much when otherwise
/*
newVertices[2] = new Vector3(newVertices[2].x, laserLength1 - hit.distance, newVertices[2].z);
newVertices[3] = new Vector3(newVertices[3].x, laserLength1 - hit.distance, newVertices[3].z);
*/
//draw ray for reference of next ray
Debug.DrawRay(hitPoint, reflect2D, Color.green);
//Debug.Log (laserLength1 - hit.distance);
//Debug.Log (hit.distance);
//
//cast next ray if 1st ray hits
//
//NEED TO FIND BETTER WAY TO DEAL WITH DISTANCE TO SUBTRACT
//FROM DISTANCE OF LAST RAY
//SEEING HOW HIT.DISTANCE IS BACKWORDS
RaycastHit2D hit2 = Physics2D.Raycast(hitPoint, incom, laserLengthDefault - hit.distance);
//RaycastHit2D hit2 = Physics2D.Raycast(hitPoint, incom, hit.distance);
if (hit2)
{
if (hit2.distance <= 0f)
{
laserLength2 = 0;
}
else
{
laserLength2 = laserLengthDefault;
}
reflect2.x = hit2.normal.x;
reflect2.y = hit2.normal.y;
incom2 = Vector3.Reflect(reflect2D, reflect2);
//for reference cast?
//sets angle from 2nd hit point...
//z is distance of refences??????
reflect2D2.x = incom2.x;
reflect2D2.y = incom2.y;
reflect2D2.z = laserLengthDefault - hit.distance;
hitPoint2 = hit2.point + hit2.normal*0.1f;
Debug.DrawRay(hitPoint2, reflect2D2, Color.red);
//Debug.Log (hit2.distance);
//RaycastHit2D hit3 = Physics2D.Raycast(hitPoint2, incom2, laserLengthDefault - (hit.distance + hit2.distance));
RaycastHit2D hit3 = Physics2D.Raycast(hitPoint2, incom2, laserLengthDefault - (hit.distance + hit2.distance));
if (hit3)
{
if (hit3.distance <= 0f)
{
laserLength3 = 0;
}
else
{
laserLength3 = laserLengthDefault;
}
reflect3.x = hit3.normal.x;
reflect3.y = hit3.normal.y;
incom3 = Vector3.Reflect(reflect2D2, reflect3);
reflect2D3.x = incom3.x;
reflect2D3.y = incom3.y;
reflect2D3.z = laserLengthDefault - hit.distance;
hitPoint3 = hit3.point + hit3.normal*0.1f;
Debug.DrawRay(hitPoint3, reflect2D3, Color.yellow);
//Debug.Log (laserLengthDefault - hit2.distance);
//Debug.Log(hit.distance+hit2.distance+hit3.distance);
}
}
}
else
{
/*
newVertices[2] = new Vector3(newVertices[2].x, laserLengthDefault - hit.distance, newVertices[2].z);
newVertices[3] = new Vector3(newVertices[3].x, laserLengthDefault - hit.distance, newVertices[3].z);
*/
}
}
I wanna say it has to possibly be due to one of the axis of the rotate to mouse position doing a flip as it rotates around…
My thought at the moment is to find the degrees at which the reverse happens and change the math to compensate…