Hi everyone i'm having some odd errors that started appearing when writing this script, i'm normally ok at debugging but im at an end with this one and wonder if some extra eyes could help me understand what the problem is.
using UnityEngine; using System.Collections;
public class RayTest : MonoBehaviour { #region Main Fields
public float rayLength = 25.0f;
//PlayerMove
public float playerMoveSpeed = 150;
//public Transform placeHolder;
//Creates a ray object named aimRay
private Ray aimRay;
private Ray aimRayBounce;
//Creates RaycastHits to store information from objects colliding with the ray
static RaycastHit rayHit01;
// For the gameobject and hit transform
private Transform playerTrans;
Transform reflectedTrans;
//Raybounce Vector
private Vector3 rayBounceVector = rayHit01.transform.position;
#endregion
#region aimLine Fields
static LineRenderer aimLine;
#endregion
void Awake ()
{
//Get this transform (might not need)
playerTrans = this.GetComponent<Transform>();
}
void Start ()
{
//aimLine (the drawn line) Initialisation
aimLine = gameObject.AddComponent<LineRenderer>();
aimLine.SetVertexCount(3);
aimLine.SetWidth(0.14f, 0.14f);
aimLine.SetColors(Color.red, Color.red);
aimLine.useWorldSpace = true;
}
void Update ()
{
#region PlayerMovement
//Player Rotation Method Call (TEMP)
PlayerRotate();
#endregion
//cast Debug.DrawRay for checking ray length
Debug.DrawRay(transform.position, transform.up * rayLength);
//--------------------------------------------------------------------------
//Raycast Code
aimRay = new Ray(playerTrans.position,transform.forward);
if(Physics.Raycast(aimRay.origin,transform.up, out rayHit01, rayLength))
{
Debug.Log("You hit a collider");
Debug.Log("Name: " + rayHit01.collider.name +
" - Position: "+ rayBounceVector +
" - Distance: " + rayHit01.distance +
" - Normals: " + rayHit01.normal);
aimLine.enabled = true;
Debug.DrawRay(rayHit01.point, Vector3.Reflect(rayHit01.point, rayHit01.normal));
}
else
{
//The ray is not colliding with anything
Debug.Log("Not colliding");
//aimLine01
aimLine.enabled = false;
}
//aimLine01
aimLine.SetPosition(0, transform.position);
aimLine.SetPosition(1, rayHit01.point);
//TEMP: sets the final position to be the placeholder when finnished there should be an if
//statement which askes if a colliders been hit, set to rayhitpoint else set to the end of the
//rays length with ... ray.getpoint
//aimLine.SetPosition(2, placeHolder.position);
}
// Rotate: Player move script (TEMP)
void PlayerRotate ()
{
float amtToMove = Input.GetAxis("Horizontal") * playerMoveSpeed * Time.deltaTime;
transform.Rotate(0,0,amtToMove,Space.Self);
}}