Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: C#
Posted by: Charles McCann
Added: Jan 2, 2017 2:09 PM
Modified: Jan 2, 2017 4:03 PM
Views: 134
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. public class WeaponRifle : WeaponBase
  6. {
  7.     public override void OnOwner(PlayerCommand cmd, BoltEntity entity)
  8.     {
  9.         if (entity.isOwner)
  10.         {
  11.             IPlayerState state = entity.GetState<IPlayerState>();
  12.             PlayerController controller = entity.GetComponent<PlayerController>();
  13.  
  14.             Vector3 pos;
  15.             Quaternion look;
  16.  
  17.             // this calculate the looking angle for this specific entity
  18.             PlayerCamera.instance.CalculateCameraAimTransform(entity.transform, state.pitch, out pos, out look);
  19.  
  20.             // display debug
  21.             Debug.DrawRay(pos, look * Vector3.forward);
  22.  
  23.             //NEW
  24.             float impactDistance = 100.0f;
  25.             Ray r = new Ray(pos, look * Vector3.forward);
  26.             RaycastHit rh;
  27.             if (Physics.Raycast(r, out rh))
  28.             {
  29.                 impactDistance = rh.distance;
  30.                 Debug.Log("Unity Raycast impactDistance: " + impactDistance);
  31.                 Debug.Log("pos: " + pos + "  rh.point: " + rh.point + " V3.Distance: " + Vector3.Distance(pos, rh.point));
  32.             }
  33.  
  34.  
  35.             using (var hits = BoltNetwork.RaycastAll(new Ray(pos, look * Vector3.forward), cmd.ServerFrame))
  36.             {
  37.                 for (int i = 0; i < hits.count; ++i)
  38.                 {
  39.                     var hit = hits.GetHit(i);
  40.                    
  41.  
  42.                     //NEW: watch for impacts.  If hit other collider firtst, don't hit player.
  43.                     if (hit.distance < impactDistance)
  44.                     {
  45.                         Debug.Log("Bolt hit.distance: " + hit.distance );
  46.                         Debug.Log("pos: " + pos + "  hit.body.transform.position: " + hit.body.transform.position +" V3.Distance: " + Vector3.Distance(pos, hit.body.transform.position));
  47.  
  48.                         var serializer = hit.body.GetComponent<PlayerController>();
  49.                         if ((serializer != null) && (serializer.state.team != state.team))
  50.                         {
  51.                             serializer.ApplyDamage(controller.activeWeapon.damagePerBullet);
  52.                         }
  53.  
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.     }
  59.  
  60.     //Unchanged
  61.     public override void Fx(BoltEntity entity)
  62.     {
  63.         Vector3 pos;
  64.         Quaternion rot;
  65.         PlayerCamera.instance.CalculateCameraAimTransform(entity.transform, entity.GetState<IPlayerState>().pitch, out pos, out rot);
  66.  
  67.         Ray r = new Ray(pos, rot * Vector3.forward);
  68.         RaycastHit rh;
  69.  
  70.         if (Physics.Raycast(r, out rh) && impactPrefab)
  71.         {
  72.             var en = rh.transform.GetComponent<BoltEntity>();
  73.             var hit = GameObject.Instantiate(impactPrefab, rh.point, Quaternion.LookRotation(rh.normal)) as GameObject;
  74.  
  75.             if (en)
  76.             {
  77.                 hit.GetComponent<RandomSound>().enabled = false;
  78.             }
  79.  
  80.             if (trailPrefab)
  81.             {
  82.                 var trailGo = GameObject.Instantiate(trailPrefab, muzzleFlash.position, Quaternion.identity) as GameObject;
  83.                 var trail = trailGo.GetComponent<LineRenderer>();
  84.  
  85.                 trail.SetPosition(0, muzzleFlash.position);
  86.                 trail.SetPosition(1, rh.point);
  87.             }
  88.         }
  89.  
  90.         GameObject go = (GameObject)GameObject.Instantiate(shellPrefab, shellEjector.position, shellEjector.rotation);
  91.         go.GetComponent<Rigidbody>().AddRelativeForce(0, 0, 2, ForceMode.VelocityChange);
  92.         go.GetComponent<Rigidbody>().AddTorque(new Vector3(Random.Range(-32f, +32f), Random.Range(-32f, +32f), Random.Range(-32f, +32f)), ForceMode.VelocityChange);
  93.  
  94.         // show flash
  95.         muzzleFlash.gameObject.SetActive(true);
  96.     }
  97. }

1 comment

Kevin L 1 year ago
Can you post your "WeaponBase" script too? Thanks!

Write a comment