Question by
S.John · Jan 11, 2016 at 03:52 PM ·
linerendererraycasthit2daimingreflect
problem reflecting a ray
Hello,
I am having trouble reflecting a ray for a simple aiming system (ike a laser with one bounce on a rigidbody, no physic calculations or what so ever).
I kinda have it working but the angle is off. I am struggling with this for a day now so i think i need some extra eyes!
using UnityEngine;
using System.Collections;
public class AimBeam : MonoBehaviour {
public Transform ball;
public LineRenderer lineRenderer;
public float rayLength;
public Vector3 reflectDirection;
void Start () {
ball = GameObject.Find("Ball").transform;
lineRenderer = this.GetComponent<LineRenderer>();
}
void Update () {
//Face Beam In Ball Direction
transform.rotation = ball.rotation;
//Draw Ray
Vector2 rayDir = transform.TransformDirection (Vector2.right) * rayLength;
RaycastHit2D hit = Physics2D.Raycast (transform.position, rayDir);
Debug.DrawRay (transform.position, rayDir, Color.green);
//Set LineRender Pos 0 & 1
lineRenderer.SetPosition (0, this.transform.position);
lineRenderer.SetPosition (1, rayDir);
//CHeck if Ray hits Collider
if (hit.collider) {
Vector3 hitPoint = hit.point;
lineRenderer.SetPosition (1, hitPoint);
//Draw Normal line
Debug.DrawRay (hit.point, hit.normal*3, Color.blue);
//Calculate reflect direction
reflectDirection = Vector3.Reflect (rayDir, hit.normal);
Debug.Log (reflectDirection);
//Set reflect direction in lineRenderer
lineRenderer.SetPosition (2, reflectDirection);
} else {
lineRenderer.SetPosition (1, rayDir);
}
}
}
The problem has to be somewhere in this line "reflectDirection = Vector3.Reflect (rayDir, hit.normal);" What am i doing wrong? it has to be something with the input direction i think but i can't figure it out.
Thanks in advanced.
Sr.J.
schermafbeelding-2016-01-09-om-171600.png
(155.4 kB)
schermafbeelding-2016-01-09-om-171607.png
(155.2 kB)
Comment