- Home /
Question by
TJeyAGames · Jan 31, 2020 at 09:14 PM ·
c#raycastcurve
Robot isn't shooting twice
I made a robot that is supposed to shoot at the player once the player is recognized/in sight. The shoot animation then plays. In this animation two lasers are shot: the first one from the left hand of the robt, the second from the right. I use a curve to determine when the shots occurr and raycasts from each hand in order to determine if the player has been hit. When I run the scene, the robot recognizes the player but only shoots from his left hand (only shots from the left hand do damage and the effects of the right laser don't work either). Does anybody know how to fix this?`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemyShooting : MonoBehaviour {
public float damage = 10f;
public float flashIntensity = 3f;
public float fadeSpeed = 10f;
public GameObject Player;
public GameObject fx_laserShot_left;
public GameObject fx_laserShot_right;
public Transform laserLeftDirection;
public Transform laserRightDirection;
private Animator anim;
private LineRenderer laserShotLineLeft;
private LineRenderer laserShotLineRight;
private Light laserShotLightRight;
private Light laserShotLightLeft;
private SphereCollider col;
private PlayerHealth playerHealth;
public bool shooting;
public bool leftShot;
public bool rightShot;
public float pewRange;
void Awake (){
anim = GetComponent<Animator> ();
laserShotLineLeft = fx_laserShot_left.GetComponent<LineRenderer>();
laserShotLineRight = fx_laserShot_right.GetComponent<LineRenderer>();
laserShotLightRight = fx_laserShot_right.GetComponent<Light>();
laserShotLightLeft = fx_laserShot_left.GetComponent<Light>();
col = GetComponent<SphereCollider> ();
playerHealth = Player.gameObject.GetComponent<PlayerHealth> ();
pewRange = col.radius;
}
// Update is called once per frame
void Update () {
float shot = anim.GetFloat(Animator.StringToHash("Shot"));
if (shot > 0.1f && !shooting) {
shooting = true;
if (shot == 1f)
ShootLeft ();
if (shot == 2f)
ShootRight ();
}
if (shot < 1f) {
shooting = false;
}
if (shot != 1f) {
laserShotLineLeft.enabled = false;
leftShot = false;
}
laserShotLightLeft.intensity = Mathf.Lerp (laserShotLightLeft.intensity, 0f, fadeSpeed * Time.deltaTime);
laserShotLightRight.intensity = Mathf.Lerp (laserShotLightLeft.intensity, 0f, fadeSpeed * Time.deltaTime);
if (shot != 2f) {
laserShotLineRight.enabled = false;
rightShot = false;
}
}
public void ShootLeft (){
Physics.Raycast(fx_laserShot_left.transform.position, fx_laserShot_left.transform.forward, out RaycastHit hit, pewRange);
if (hit.transform == Player.transform)
{
playerHealth.TakeDamage(damage);
}
//Effect
laserShotLineLeft.SetPosition(0, laserShotLineLeft.transform.position);
laserShotLineLeft.SetPosition(1, laserLeftDirection.transform.position);
laserShotLineLeft.enabled = true;
laserShotLightLeft.intensity = flashIntensity;
}
public void ShootRight(){
Physics.Raycast(fx_laserShot_right.transform.position, fx_laserShot_right.transform.forward, out RaycastHit hit, pewRange);
if (hit.transform == Player.transform)
{
playerHealth.TakeDamage(damage);
}
//Effect
laserShotLineRight.SetPosition(0, laserShotLineRight.transform.position);
laserShotLineRight.SetPosition(1, laserRightDirection.transform.position);
laserShotLineRight.enabled = true;
laserShotLightRight.intensity = flashIntensity;
}
} `
Comment
Your answer
