// Use this for initialization void Start() { mColliders = GetComponents <Collider2D>(); //Find colliders Debug.Assert(mColliders.Length > 0, "Collider(s) missing"); //Show error if not found mFakePhysics = GetComponent <FakePhysics>(); //Add required Fake physics in code Debug.Assert(mFakePhysics != null, "Needs FakePhysics"); //Check its added ok mExplosion = GetComponentInChildren <Explosion>(); Debug.Assert(mExplosion != null, "No Explosion found in hierachy"); foreach (var mCollider in mColliders) //Step through all the colliders { mCollider.isTrigger = true; //Use code to turn collider to triggers } mSR = GetComponent <SpriteRenderer>(); //Get SpriteRenderer Debug.Assert(mSR != null, "SpriteRenderer missing"); //Show error if not found mRB2D = gameObject.AddComponent <Rigidbody2D>(); //Add RB2D in code Debug.Assert(mRB2D != null, "Needs Rigidbody2D"); //Check its added ok mRB2D.isKinematic = true; //Make it non physics in code, so triggers still work, but no forces act on it mAngle = Random.Range(-45.0f, 45.0f); //Set a random rotation rate if (RandomVelocity) //If IDE variable is set then give an initial random velocity { mFakePhysics.mVelocity = FakePhysics.RandomDirection() * Random.Range(1.0f, 5.0f); } }
private void OnTriggerEnter2D(Collider2D collision) { FakePhysics tFF = collision.GetComponent <FakePhysics>(); //Get FakePhysics component Debug.AssertFormat(tFF != null, "Other object {0} is not FakePhysics", collision.name); //Ensure its valid CollidedWith(tFF); //Pass to collision handler }
//We are using triggers, so this gets called on overlap private void OnTriggerEnter2D(Collider2D vCollision) { FakePhysics tOtherObject = vCollision.gameObject.GetComponent <FakePhysics>(); Debug.Assert(tOtherObject != null, "Other Object is not FakePhysics compatible"); ObjectHit(tOtherObject); }
FakePhysics mParentFakePhysics; //Cached Parent FakePhysics // Use this for initialization void Start() { //We need some values from Parent's Physics, so grab them mParentFakePhysics = transform.root.gameObject.GetComponent <FakePhysics>(); //Get Parent FakePhysics Debug.Assert(mParentFakePhysics != null, "Could find Parent FakePhysics"); Debug.Assert(Bullet != null, "Could find Bullet Prefab"); }
Collider2D mCollider2D; //Cached copy of Collider // Use this for initialization void Start() { mFakePhysics = GetComponent <FakePhysics>(); //Add FakePhysics Component in code Debug.Assert(mFakePhysics != null, "Needs FakePhysics"); //Check its added ok mCollider2D = GetComponent <Collider2D>(); Debug.Assert(mCollider2D != null, "Bullet needs a collider"); mCollider2D.isTrigger = true; //set Trigger in code so we don't forget Destroy(gameObject, TimeToLive); //Time to live }
Rigidbody2D mRB2D; //Cached Variable for quick access void Start() { mFakePhysics = GetComponent <FakePhysics>(); //Add FakePhysics Component in code Debug.Assert(mFakePhysics != null, "Needs FakePhysics"); //Check its added ok mFakePhysics.MaxSpeed = 10.0f; //Tell FakePhysics to clamp speed mRB2D = gameObject.AddComponent <Rigidbody2D>(); //Add RB2D in code Debug.Assert(mRB2D != null, "Needs Rigidbody2D"); //Check its added ok mRB2D.isKinematic = true; //Make it non physics in code, so triggers still work, but no forces act on it }
Rigidbody2D mRB2D; //Cached Variable for quick access // Use this for initialization void Start() { mFakePhysics = GetComponent <FakePhysics>(); //Add Fake Physics in code Debug.Assert(mFakePhysics != null, "Needs FakePhysics"); //Check its added ok mFakePhysics.MaxSpeed = 10.0f; //Set UFO Max Speed mFakePhysics.mVelocity = FakePhysics.RandomDirection() * Speed; //Set UFO Random direction mRB2D = gameObject.AddComponent <Rigidbody2D>(); //Add RB2D in code Debug.Assert(mRB2D != null, "Needs Rigidbody2D"); //Check its added ok mRB2D.isKinematic = true; //Make it non physics in code, so triggers still work, but no forces act on it }
//Make a random direction change every few seconds void UpdateDirection() { if (mTimeOut <= 0) { mFakePhysics.mVelocity = FakePhysics.RandomDirection() * Speed; //Get Random direction an scale with speed mTimeOut = Random.Range(1, 10); //Set new random timeout Fire[] tFirePoint = GetComponentsInChildren <Fire>(); foreach (var tFP in tFirePoint) { tFP.DoFire(); } } else { mTimeOut -= Time.deltaTime; //subtract the time since last update } }
void Update() { if (mCooldownCount <= 0) { if (Input.GetButton("Fire1")) { BulletFP tBullet = GM.SpawnPrefab(GM.SpawnIDs.Bullet, transform.position, 0).GetComponent <BulletFP>(); FakePhysics tParentFP = GetComponentInParent <FakePhysics>(); tBullet.mVelocity = (Vector2)transform.up * BulletSpeed; //Initial Velocity tBullet.mVelocity += tParentFP.mVelocity; //Player ship velocity mCooldownCount = CoolDown; } } else { mCooldownCount -= Time.deltaTime; } }
void EnterNewState(State vNewState) { switch (vNewState) { case State.Search: TrackedObject = null; SeenObject = null; break; case State.TargetLocked: TrackedObject = SeenObject; mLostCount = MaxLostCount; break; case State.TargetMemory: SeenObject = null; mLostCount = MaxLostCount; break; } }
protected override void CollidedWith(FakePhysics vOtherFF) { Debug.LogFormat("RockSmall hit by {0}", vOtherFF.name); //RockMedium specifc code //We do not call parent as we will handle if (vOtherFF is BulletFP) { //Were we hit by a bullet Destroy(gameObject); //Destroy Asteroid Destroy(vOtherFF.gameObject); //Destroy Bullet GM.SpawnPrefab(GM.SpawnIDs.Explosion, transform.position, Random.Range(-180, 180)); // Exposion GM.sSingleton.Score += 200; //Give player score } else if (vOtherFF is PlayerFP) { Destroy(gameObject); //Destroy Asteroid GM.SpawnPrefab(GM.SpawnIDs.Explosion, transform.position, Random.Range(-180, 180)); // Exposion GM.sSingleton.Score += 20; //Give less player score for crashing GM.sSingleton.Health = Mathf.Clamp(GM.sSingleton.Health - 0.015f, 0.0f, 1.0f); //Reduce Health, but clamp } }
public void DoFire() { Vector2 tFireDirection = transform.up; //Take fire position rotation as fire angle GameObject mBulletGO = Instantiate(Bullet, transform.position, Quaternion.identity); Debug.Assert(mBulletGO != null, "Could not create Bullet from prefab"); FakePhysics tBulletFakePhysics = mBulletGO.GetComponent <FakePhysics>(); //Fake Physics for Buller Debug.Assert(tBulletFakePhysics != null, "Could find Bullet FakePhysics Component"); Bullet tBullet = mBulletGO.GetComponent <Bullet>(); Debug.Assert(tBullet != null, "Could not find Bullet Component"); //Bullet velocity relative to ship tBulletFakePhysics.mVelocity += mParentFakePhysics.mVelocity + tFireDirection * tBullet.Speed; //Add ship velocity to bullet tBulletFakePhysics.MaxSpeed += mParentFakePhysics.MaxSpeed; //Allow playership speed + Bullet speed }
void ProcessState() { switch (mCurrentState) { case State.Search: SeenObject = CanSee(transform.position, transform.forward * ScanDistance); //Try to find the enemy if (SeenObject != null) { NewState(State.TargetLocked); } break; case State.TargetLocked: { FakePhysics tObject = CanSee(transform.position, transform.forward * ScanDistance); //Try to find the enemy if (tObject == null || tObject != SeenObject) { NewState(State.TargetMemory); } } break; case State.TargetMemory: { FakePhysics tObject = CanSee(transform.position, transform.forward * ScanDistance); //Try to find the enemy if (tObject == null || tObject != SeenObject) { if (mLostCount > 0) { mLostCount--; } else { NewState(State.Search); } } } break; default: break; } }
//Use Raycast to find object in line of sight, using physics Layermask FakePhysics CanSee(Vector3 vPosition, Vector3 vDirection) { RaycastHit tHit; if (Physics.Raycast(vPosition, vDirection, out tHit, vDirection.magnitude, mIgnoredLayers, QueryTriggerInteraction.Ignore)) { GameObject tGO = tHit.collider.gameObject; FakePhysics tFakePhysics = tGO.GetComponent <FakePhysics>(); if (tFakePhysics != null) { Debug.DrawLine(vPosition, tHit.point, Color.red, 1.0f); //Viable Target return(tFakePhysics); } else { Debug.DrawLine(vPosition, tHit.point, Color.yellow, 0.1f); //Non viable target } } else { Debug.DrawLine(vPosition, vPosition + vDirection, Color.grey, 0.1f); //Nothing Hit } return(null); }
protected override void CollidedWith(FakePhysics vOtherFF) { if (vOtherFF is BulletFP) { //Were we hit by a bullet Destroy(gameObject); //Destroy Asteroid Destroy(vOtherFF.gameObject); //Destroy Bullet GM.SpawnPrefab(GM.SpawnIDs.Explosion, transform.position, Random.Range(-180, 180)); // Exposion GM.SpawnPrefab(GM.SpawnIDs.AsteroidSmall, transform.position, Random.Range(-180, 180)); //Medium Rock GM.SpawnPrefab(GM.SpawnIDs.AsteroidSmall, transform.position, Random.Range(-180, 180)); //Medium Rock GM.SpawnPrefab(GM.SpawnIDs.AsteroidSmall, transform.position, Random.Range(-180, 180)); //Medium Rock GM.sSingleton.Score += 150; //Give player score } else if (vOtherFF is PlayerFP) { Destroy(gameObject); //Destroy Asteroid GM.SpawnPrefab(GM.SpawnIDs.Explosion, transform.position, Random.Range(-180, 180)); // Exposion GM.SpawnPrefab(GM.SpawnIDs.AsteroidSmall, transform.position, Random.Range(-180, 180)); //Medium Rock GM.SpawnPrefab(GM.SpawnIDs.AsteroidSmall, transform.position, Random.Range(-180, 180)); //Medium Rock GM.SpawnPrefab(GM.SpawnIDs.AsteroidSmall, transform.position, Random.Range(-180, 180)); //Medium Rock GM.sSingleton.Score += 150; //Give less player score for crashing GM.sSingleton.Health = Mathf.Clamp(GM.sSingleton.Health - 0.025f, 0.0f, 1.0f); //Reduce Health, but clamp } }
//Default Collision handler protected virtual void CollidedWith(FakePhysics vOtherFF) { Debug.LogFormat("Collision between {0} and {1}", name, vOtherFF.name); //Print Message }
protected override void CollidedWith(FakePhysics vOtherFF) { Debug.LogFormat("Player hit by {0}", vOtherFF.name); //Player specifc code //We do not call parent as we will handle }
//virtual functions can be overridded in derived classes protected virtual void ObjectHit(FakePhysics vOtherObject) { Debug.LogFormat("{0} hit by {1}", name, vOtherObject.name); //Just print it for now }