private void Update() { Targetable target = targeter.GetTarget(); if (target == null) { return; } if (!CanFireAtTarget()) { return; } //calculate rotation towards target Quaternion targetRotation = Quaternion.LookRotation(target.transform.position - transform.position); transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); //calculating fire rate if (Time.time > (1 / fireRate) + lastFiredTime) { //calculate projectile rotation Quaternion projectileRotation = Quaternion.LookRotation(target.GetAimAtPoint().transform.position - projectileSpawnPoint.position); //we can now shoot GameObject projectileInstance = Instantiate(projectilePrefab, projectileSpawnPoint.position, projectileRotation); //spawn the projectile on server and give it's proper permission by connectioToClient NetworkServer.Spawn(projectileInstance, connectionToClient); //set time when we fired last lastFiredTime = Time.time; } }
private void Update() { Targetable target = targeter.GetTarget(); if (target == null) { return; } if (!CanFireAtTarget()) { return; } Quaternion targetRotation = Quaternion.LookRotation(target.transform.position - transform.position); transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); if (Time.time > (1 / fireRate) + lastFireTime) { Quaternion projectileRotation = Quaternion.LookRotation(target.GetAimAtPoint().position - projectileSpawnPoint.position); GameObject projectileInstance = Instantiate(projectilePrefab, projectileSpawnPoint.position, projectileRotation); NetworkServer.Spawn(projectileInstance, connectionToClient); lastFireTime = Time.time; } }
private void Fire() { if (Time.time > (1 / fireRate) + lastFireTime)//Calculate Fire Rate { //Firing Quaternion projectileRotation = Quaternion.LookRotation(target.GetAimAtPoint().position - projectileSpawnPoint.position); //To Do -----------Instantiate Projectile After Rotation Process Finished----------- GameObject projectileInstance = Instantiate(projectilePrefab, projectileSpawnPoint.position, projectileRotation); //Before That Part We Instantiating Projectile Only On Server, To Instantiate Also On Clients We Use NetworkServer Command NetworkServer.Spawn(projectileInstance, connectionToClient); lastFireTime = Time.time; } }
private void Update() { Targetable target = targeter.GetTarget(); if (target == null) { return; } //Debug.Log($"targeter targeterAttackType {targeter.targeterAttackType}"); if (targeter.targeterAttackType != Targeter.AttackType.Shoot) { return; } if (!CanFireAtTarget()) { return; } Quaternion targetRotation = Quaternion.LookRotation(target.transform.position - transform.position); transform.rotation = Quaternion.RotateTowards( transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); if (Time.time > (1 / fireRate) + lastFireTime) { Quaternion projectileRotation = Quaternion.LookRotation( target.GetAimAtPoint().position - projectileSpawnPoint.position); GameObject projectileInstance = Instantiate( projectilePrefab, projectileSpawnPoint.position, projectileRotation); //Debug.Log($"Unit Firing projectilePrefab {projectilePrefab} projectileInstance {projectileInstance}"); //Physics.IgnoreCollision(projectilePrefab.GetComponent<Collider>(), GetComponent<Collider>()); NetworkServer.Spawn(projectileInstance, connectionToClient); lastFireTime = Time.time; } }
private void Update() { //Gets target position Targetable target = targeter.getTarget(); //if no target found do nothing if (target == null) { return; } //If cant fire at target do nothing if (!CanFireAtTarget()) { return; } //Gets target rotation Quaternion targetRotation = Quaternion.LookRotation(target.transform.position - transform.position); //Rotates unit to face target transform.rotation = Quaternion.RotateTowards( transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); //Controls fire rate relative to last time fired if (Time.time > (1 / fireRate) + lastFireTime) { //sets projectile rotation to point at target Quaternion projectileRotation = Quaternion.LookRotation( target.GetAimAtPoint().position - projectileSpawnPoint.position); //create projectile GameObject projectiltInstance = Instantiate( projectilePrefab, projectileSpawnPoint.position, projectileRotation); //Spawn projectile on network, give authority to current player NetworkServer.Spawn(projectiltInstance, connectionToClient); //update last time fired to current time lastFireTime = Time.time; } }
private void Update() { Targetable target = targeter.GetTarget(); if (target == null) { return; } if (!CanFireAtTarget(target)) { return; } // get the target rotation relative to us Quaternion targetRotation = Quaternion.LookRotation( target.transform.position - transform.position); // rotate us towards the target transform.rotation = Quaternion.RotateTowards( transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); if (Time.time > (1 / fireRate) + lastFireTime) { // get the rotation from the projectile spawner towards the target AimAtPoint Quaternion projectileRotation = Quaternion.LookRotation( target.GetAimAtPoint().position - projectileSpawnPoint.position); // instantiate the prefab on the server GameObject projectileInstance = Instantiate( projectilePrefab, projectileSpawnPoint.position, projectileRotation); // spawn projectile over the network and give ownership to this unit's owner NetworkServer.Spawn(projectileInstance, connectionToClient); projectileInstance.GetComponent <UnitProjectile>().SetDamageToDeal(damage); lastFireTime = Time.time; } }
private void Update() { Targetable target = targeter.GetTarget(); if (target == null) { return; } if (!CanFireAtTarget()) { return; } //geting vector pointing towards teh unit Quaternion targetRotation = Quaternion.LookRotation(target.transform.position - transform.position); //implementing rotating towards target with a speed and frame rate independent transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); if (Time.time > (1 / attackSpeed) + lastFireTime) { // we can now fire for (int i = 0; i < projectileSpawnPoints.Length; i++) { Quaternion projectileRotation = Quaternion.LookRotation(target.GetAimAtPoint().position - projectileSpawnPoints[i].position); GameObject projectileInstance = Instantiate(projectilePrefab, projectileSpawnPoints[i].position, projectileRotation); NetworkServer.Spawn(projectileInstance, connectionToClient); } lastFireTime = Time.time; } }