private void FixedUpdate() { if (target != null) { if (updateThisFrame) { Vector3 direction = target.GetAimPoint().position - transform.position; rigidbody.velocity = direction.normalized * speed; updateThisFrame = false; } else { updateThisFrame = true; } } }
private void Update() { Targetable target = targeter.GetTarget(); if (target == null) { return; } if (!CanFireAtTarget()) { return; } //In range of target here Quaternion targetRotation = Quaternion.LookRotation(target.transform.position - transform.position); //Rotates the unit based upon our rotation and target rotation and speed. transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); //Make sure we can fire based upon fire rate and fire timer. if (Time.time > (1 / fireRate) + lastFireTime) //If firerate is 1 we fire 1 per second. E.G how ever many per second { //Get the projectile rotation to aim at center of targeter. Quaternion projectrileRotation = Quaternion.LookRotation(target.GetAimPoint().position - projectileSpawnPoint.position); //Instantiate the projectile instance onto the Server, but not network yet. GameObject projectileInstance = Instantiate(projectilePrefab, projectileSpawnPoint.position, projectrileRotation); NetworkServer.Spawn(projectileInstance, connectionToClient); lastFireTime = Time.time; } }