本文整理汇总了C#中Unit.GetComponent方法的典型用法代码示例。如果您正苦于以下问题:C# Unit.GetComponent方法的具体用法?C# Unit.GetComponent怎么用?C# Unit.GetComponent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Unit
的用法示例。
在下文中一共展示了Unit.GetComponent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Update
// Update is called once per frame
public virtual void Update(Unit unit)
{
//If unit has arrived at destination then stop it
float distance = Vector3.Distance(unit.getDestination(), unit.transform.position);
if (distance < stoppingDistance) {
unit.GetComponent<NavMeshAgent>().destination = unit.transform.position;
unit.setDestination (unit.transform.position);
unit.changeState (new Idle());
}
if (unit.GetComponent<NavMeshAgent>().nextPosition == unit.getDestination ()) {
unit.GetComponent<NavMeshAgent>().updatePosition = false;
}
/* COMBAT (DRIVEBY STYLE) */
foreach (Collider enemy in Physics.OverlapSphere (unit.transform.position, unit.getRange(),1<<10)){
Unit enemyUnit = enemy.GetComponent<Unit>();
if (enemyUnit != null && unit.team != enemyUnit.team){ // cant we all just get along?
unit.lookAtPos(enemyUnit.transform.position);
if(unit.canFire ()){
enemyUnit.GetComponent<Health>().applyDamage(unit.getDamage());
unit.resetCooldown ();
unit.drawAttack(unit,enemyUnit, unit.renderer.material.color);
}
break;
}
}
}
示例2: Use
public override void Use(Unit self, Vector3 position)
{
var parentTcp = self.GetComponentInParent<TrolleyChargeProjectile>();
if (parentTcp)
{
self.switchOff = false;
self.transform.SetParent(parentTcp.oldParent);
self.GetComponent<NavMeshAgent>().enabled = true;
self.GetComponent<Collider>().enabled = true;
GameObject.Destroy(parentTcp.gameObject);
return;
}
self.switchOff = true;
var trolley = self.transform.Find("Babywka").Find("Trolley");
var trolleyChargeInst = GameObject.Instantiate(trolleyCharge).transform;
var projectile = trolleyChargeInst.GetComponent<TrolleyChargeProjectile>();
projectile.oldParent = self.transform.parent;
var selfPos = self.transform.position;
selfPos.y = 0;
position.y = 0;
self.transform.rotation = Quaternion.LookRotation(position - selfPos);
trolleyChargeInst.position = trolley.position;
trolleyChargeInst.rotation = trolley.rotation;
self.transform.SetParent(trolleyChargeInst);
self.GetComponent<NavMeshAgent>().enabled = false;
self.GetComponent<Collider>().enabled = false;
}
示例3: UnitSelected
// Unit selected movement on
public void UnitSelected(Unit unit)
{
if (!unit.GetComponent<SimpleUnitMove> ().CanMoveUnit)
{
unit.GetComponent<SimpleUnitMove> ().CanMoveUnit = true;
}
}
示例4: UnitDeselected
// Unit deselected movement off
public void UnitDeselected(Unit unit)
{
Debug.Log ("x");
if (unit.GetComponent<SimpleUnitMove> ().CanMoveUnit)
{
unit.GetComponent<SimpleUnitMove> ().CanMoveUnit = false;
}
}
示例5: IsDefeated
protected virtual bool IsDefeated(Unit unit)
{
Health health = unit.GetComponent<Health> ();
if (health)
return health.MinHP == health.HP;
Stats stats = unit.GetComponent<Stats> ();
return stats[StatTypes.HP] == 0;
}
示例6: IsTarget
public override bool IsTarget (Unit target)
{
if (target == null) {
return false;
}
bool hasComponent = target.GetComponent<Undead>() != null;
if (hasComponent != toggle)
return false;
Stats s = target.GetComponent<Stats>();
return s != null && s[StatTypes.HP] > 0;
}
示例7: Decide
public EnemyFootUnit.EnemyObjective Decide(Unit unit)
{
EnemyFootUnit fu = unit.GetComponent<EnemyFootUnit>();
float health = fu.GetHP();
float maxHealth = fu.GetMaxHP();
float range = fu.GetAttackRange();
Vector3 position = unit.gameObject.transform.position;
float distanceToNearestEnemy = sight.DistanceToClosestUnit();
float numEnemiesInRange = sight.NumEnemiesInRange();
float numFriendsInRange = sight.NumFriendsInRange();
//Advance = C1 * health/maxHealth + C2 * distanceToNearestEnemy.normalized/range
float C1 = 1, C2 = 1, C3 = 1, C4 = 1, C5 = 1, C6 = 1, C7 = 1;
float advanceReward = C1* health / maxHealth + C2 * distanceToNearestEnemy / range;
rewards[EnemyFootUnit.EnemyObjective.Advance] = advanceReward;
//Retreat = C3 * maxHealth/health + C4 * range/distanteToNearestEnemy.normalized + C5 * numEnemiesInRange/numFriendsInRange
float retreatReward = C3* maxHealth / health + C4 * range / distanceToNearestEnemy + C5 * numEnemiesInRange / numFriendsInRange;
rewards[EnemyFootUnit.EnemyObjective.Retreat] = retreatReward;
//GetHealth;
rewards[EnemyFootUnit.EnemyObjective.GetHealth] = 0;
//Attack = C6 * distanceToNearestEnemy + C6 * chanceToHit()
float attackReward = C6 * 1/distanceToNearestEnemy + C7 * fu.ChanceToHit() + C5 * numEnemiesInRange;
rewards[EnemyFootUnit.EnemyObjective.Attack] = attackReward;
//FindCover;
rewards[EnemyFootUnit.EnemyObjective.FindCover] = 0;
return PriorityObjective;
}
示例8: Update
// Update is called once per frame
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, raycastLength))
{
if (hit.collider.name == "TerrainMain")
{
if (Input.GetMouseButtonDown(1) && !Input.GetKey(KeyCode.LeftControl) && selectedUnit != null)
{
targetParticle.Stop();
target.transform.position = hit.point;
targetParticle.Play();
selectedUnit.MoveUnit(hit.point);
}
if (Input.GetMouseButtonDown(0) && selectedUnit != null)
{
selectedUnit.SetDeselected();
selectedUnit = null;
}
}
if (hit.collider.tag == "Unit")
{
if (Input.GetMouseButtonDown(0))
{
selectedUnit = hit.collider.gameObject.GetComponent<Unit>();
selectedUnit.GetComponent<Unit>().SetSelected();
}
}
}
Debug.DrawRay(ray.origin, ray.direction * raycastLength, Color.yellow);
}
示例9: Update
// Update is called once per frame
public virtual void Update(Unit unit)
{
// if target has died
if (unit.getTarget() == null || unit.getTarget().GetComponent<Health>().currentHealth <= 0){
unit.setTarget(null);
unit.setDestination(Vector3.zero);
unit.GetComponent<NavMeshAgent>().updatePosition = false;
unit.changeState (new Idle());
return;
}
// else, if enemy still alive
Unit lastEnemy = null;
bool inRange = false;
foreach (Collider enemy in Physics.OverlapSphere (unit.transform.position, unit.getRange(),1<<10)){
Unit enemyUnit = enemy.GetComponent<Unit>();
if (enemyUnit != null && unit.team != enemyUnit.team){
lastEnemy = enemyUnit;
if (enemyUnit == unit.getTarget()){
inRange = true;
unit.setDestination (Vector3.zero);
unit.GetComponent<NavMeshAgent>().updatePosition = false;
// attack target unit
unit.lookAtPos(enemyUnit.transform.position);
if(unit.canFire ()){
enemyUnit.GetComponent<Health>().applyDamage(unit.getDamage());
unit.resetCooldown ();
unit.drawAttack(unit,enemyUnit, unit.renderer.material.color);
}
break;
}
}
}
if (!inRange){
// for now, it finds a path every update. this will be fixed in future
unit.GetComponent<NavMeshAgent>().destination = unit.getTarget ().transform.position;
unit.setDestination(unit.getTarget ().transform.position);
unit.GetComponent<NavMeshAgent>().updatePosition = true;
if (lastEnemy != null){ // not in range of target, but in range of another enemy
unit.lookAtPos(lastEnemy.transform.position);
if(unit.canFire ()){
lastEnemy.GetComponent<Health>().applyDamage(unit.getDamage());
unit.resetCooldown ();
unit.drawAttack(unit,lastEnemy, unit.renderer.material.color);
}
}
}
}
示例10: OnUnitSelect
void OnUnitSelect(object sender, object args)
{
unit = args as Unit;
stats = unit.GetComponent<Stats>();
RemoveListeners();
this.AddObserver(OnStatDidChange, Stats.DidChangeNotification(statType), stats);
ResizeBar();
}
示例11: IsTarget
public override bool IsTarget(Unit target)
{
if (target == null) {
return false;
}
Stats s = target.GetComponent<Stats>();
return s != null && s[StatTypes.HP] <= 0;
}
示例12: OnApply
public override float OnApply(Unit defender)
{
float value = Predict(defender);
Stats s = defender.GetComponent<Stats> ();
s [StatTypes.HP] += value;
this.PostNotification(HitNotification, value);
return value;
}
示例13: Fire
/// <summary>
/// Fires the bullet
/// </summary>
/// <param name="owner">The bullet's owner</param>
/// <param name="localPosition">The bullet's starting localPosition</param>
public void Fire(Unit owner, Vector3 position)
{
transform.parent = null;
transform.position = position;
transform.LookAt(_direction);
transform.Translate(transform.forward * owner.GetComponent<FPSController>().MoveDirection.magnitude * Time.deltaTime, Space.World);
Activate();
networkView.RPC("FireRemote", RPCMode.OthersBuffered, Network.player, position, _direction);
}
示例14: OnApply
public override float OnApply(Unit defender)
{
Stats s = defender.GetComponent<Stats> ();
if (s [StatTypes.HP] > 0) {
return 0;
}
float value = s [StatTypes.HP] = Predict (defender);
this.PostNotification(HitNotification, 0);
return value;
}
示例15: isInSelectionBox
bool isInSelectionBox(Unit unit)
{
bool result = false;
if (unit.GetComponent<Renderer>().isVisible)
{
Vector3 camPos = Camera.main.WorldToScreenPoint(unit.transform.position);
camPos.y = Screen.height - camPos.y;
result = selection.Contains(camPos);
}
return result;
}