override protected async void HandleShootPerformed(InputAction.CallbackContext c) { var buttonControll = c.control as ButtonControl; if (buttonControll.wasPressedThisFrame) { // if player can shoot face to Crosshair first and spawn trajectory and bullet if (Timer.IsFinished && !bShootPerformed) { Parent.ChangeState(EHumanoidState.SHOOT_START); await Parent.LookAtCrosshairAsync(); hs = LeanPool.Spawn(BulletPrefab).GetComponent <Hitscan>(); RayAttr passAttr = new RayAttr(ShootPoint.position, (Crosshair.position - ShootPoint.position).normalized, attr.Dis); hs.UpdateTrajectory(passAttr, this); bShootPerformed = true; } } if (buttonControll.wasReleasedThisFrame) { // if player released shoot button and have shot bullet before update the bullet if (bShootPerformed && Timer.IsFinished) { bShootPerformed = false; Timer.Reset(); Parent.LookAtCrosshair(); RayAttr passAttr = new RayAttr(ShootPoint.position, (Crosshair.position - ShootPoint.position).normalized, attr.Dis); hs.Fire <RayAttr>(passAttr, this); Parent.ChangeState(EHumanoidState.SHOOT_END); hs = null; } } }
void Update() { // if player keep pressing shoot button keep update trajectory and keep player face to crosshair if (bShootPerformed && hs) { Parent.LookAtCrosshair(); RayAttr passAttr = new RayAttr(ShootPoint.position, (Crosshair.position - ShootPoint.position).normalized, attr.Dis); hs.UpdateTrajectory(passAttr, this); } }
/// <summary> /// Call this method to update trajectory color and postion /// </summary> /// <param name="props">Props for trajectory</param> /// <param name="weapon">Which weapon should this bullet belongs to</param> public void UpdateTrajectory(RayAttr props, Weapon weapon) { this.Weapon = weapon; Vector3 scale = transform.localScale; scale.z = props.Dis; transform.localScale = scale; transform.position = props.Pos; transform.rotation = Quaternion.LookRotation(props.Dir); rend.material.SetColor("Bullet_Color", Weapon.SplashColor); rend.material.SetFloat("Intensity", Weapon.Intensity); }
/// <summary> /// Overwrite this method to define how bullet detect PaintableObj /// </summary> /// <param name="props">props include everything bullet need</param> /// <param name="weapon">which weapon does this bullet belongs to</param> /// <typeparam name="T">type of props inherited from HitAttr</typeparam> override public void Fire <T> (T props, Weapon weapon) { this.Weapon = weapon; RayAttr rayProps = props as RayAttr; UpdateTrajectory(rayProps, weapon); RaycastHit hit; if (Physics.Raycast(rayProps.Pos, rayProps.Dir, out hit, rayProps.Dis)) { if (hit.collider.gameObject.tag != "Paintable") { return; } PaintableObj obj = hit.collider.GetComponent <PaintableObj> ( ); if (obj) { int index = Weapon.GetRandomSplashTexIndex( ); obj.Paint(hit.textureCoord, Weapon.SplashColor, Weapon.SplashTex[index], Weapon.SplashTexColors[index]); } } LeanPool.Despawn(this.gameObject); }