Hi guys,
Im trying to draw a raycast directly out in front of the player I am instantiating. This was 100% functioning correctly until I changed my player controller to work with an Xbox controller instead of the mouse and keyboard. Now instead of the Raycast being projected straight out in front of the player, it often gets projected at different angles. I confirmed this by using Debug.DrawLine(transform.position, forwardVector). If I leave my game scene run, the way the raycast is facing constantly changes as if both it and the player are rotating at different speeds. At some moment the raycast will face straight out, but a while later it may be off at 90 degrees to the player.
Does anyone know what caused this issue or why this happens? Could it be an issue with deadzones?
This is the script that is attached to the Player.
private int damage;
private float distanceBetween;
private int rayLength = 5;
// Update is called once per frame
void Update () {
/*If the fireButton is pressed,a raycast is sent out front the front of the player. The raycast is like an invisible
* straight line. If this line hits an object, (ie if something is in front of the player), it then checks to
* see what that object is. It does this by checking the gameobjects tag.
*/
if(Input.GetButtonDown("Fire1"))
{
Debug.Log("Inside fire loop");
RaycastHit hit;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
damage = Random.Range(0,50);
Debug.DrawRay(transform.position, fwd);
if(Physics.Raycast(transform.position, fwd,out hit, rayLength))
{
/*If the raycast hits a tree, the player is within range of the tree. The damage value is then
* sent to the ApplyDamage function which is in a scipt attached to the tree.
*/
if(hit.collider.gameObject.tag == "tree")
{
Debug.Log("Chopping tree: " + damage);
hit.transform.SendMessage("ApplyDamage", damage,SendMessageOptions.DontRequireReceiver);
}
else{
Debug.Log("Not chopping a tree");
}
}
}
}