Why is my Turret not updating the current enemy? - c#

My problem is that, my turret locates the first enemy, but when it dies, OR, another enemy gets closer, the turret continues to focus on the last position of the first enemy.
The goal is to update the turrets target, to the enemy that is closest to its own transform . At some point I would like to extend that transform, to a player designated area.
private Transform target;
float range = 5f;
float distanceToAttackEnemy;
float shortestDistance = Mathf.Infinity;
[SerializeField] GameObject turretTower;
[SerializeField] GameObject turretProjektile;
[SerializeField] Transform bulletSpawn;
List<GameObject> listOfEnemies = new List<GameObject>();
GameObject nearestEnemy = null;
// Update is called once per frame
void Update()
{
CheckingForClosestEnemyInList();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == ("Enemy"))
{
listOfEnemies.Add(other.gameObject);
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.tag == ("Enemy"))
{
listOfEnemies.Remove(other.gameObject);
}
}
void CheckingForClosestEnemyInList()
{
foreach(GameObject enemy in listOfEnemies)
{
float distBetweenPlayerAndEnemy = Vector3.Distance(transform.position, enemy.transform.position);
if (distanceToAttackEnemy < shortestDistance)
{
shortestDistance = distanceToAttackEnemy;
nearestEnemy = enemy;
}
}
if (nearestEnemy != null && shortestDistance <= range)
{
var dir = nearestEnemy.transform.position - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90;
turretTower.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
else
{
nearestEnemy = null;
}
}
}
Any and all help would be greatly appreciated.

Related

Cannot implicitly convert type 'UnityEngine.GameObject' to 'GameObject'

Hello everyone and thanks in advance for your answers!
I am a beginner in unity, coding my second game after i finshed a couple of tutorials.
Since today, i noticed that suddenly all my "GameObjects" have a "UnityEngine." in front of them.
I have no idea how that happened, and it is also not just in one script, but all of them. Heres an example of it:
UnityEngine.GameObject item = (UnityEngine.GameObject)Instantiate(itemGOList[i], spawnTransform, spawnRotation);
This worked fine before without the "UnityEngine.", but now it only works when its written this way.
Do you know how this could have happened and how to revert it?
This is one of the scripts:
using UnityEngine;
using UnityEngine.EventSystems;
public class Turret : MonoBehaviour
{
private Transform target;
private Enemy targetEnemy;
[Header("General")]
public float range = 10f;
[Header("Use Bullets/missiles (default)")]
public UnityEngine.GameObject bulletPrefab;
public float fireRate = 1f;
private float fireCountdown = 0f;
public AudioClip shootingSound;
private AudioSource audioSource;
[Header("Use Laser (default)")]
public bool useLaser = false;
public int damageOverTime = 20;
public float slowAmount = .5f;
public LineRenderer lineRenderer;
public ParticleSystem impactEffect;
public Light impactLight;
[Header("Unity Setup Fields")]
public string enemyTag = "Enemy";
public Transform partToRotate;
public float turnSpeed = 10f;
public Transform firePoint;
void Start()
{
InvokeRepeating(nameof(UpdateTarget), 0f, 0.3f); // Call the UpdateTarget Method after 0 seconds, then repeat every 0.3 seconds.
audioSource = GetComponent<AudioSource>(); //Puts the AudioSource of this GO (the turret) into the variable audioSource.
}
void UpdateTarget ()
{
UnityEngine.GameObject[] enemies = UnityEngine.GameObject.FindGameObjectsWithTag(enemyTag); // Find all enemies (and store in a list?).
float shortestDistance = Mathf.Infinity; // Create a float for the shortest distance to an enemy.
UnityEngine.GameObject nearestEnemy = null; // Create a variable which stores the nearest enemy as a gameobject.
foreach (UnityEngine.GameObject enemy in enemies) // Loop through the enemies array.
{
float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position); //Get the distance to each enemy and stores it.
if (distanceToEnemy < shortestDistance) // If any of the enemies is closer than the original, make this distance the new shortestDistance and the new enemy to the nearestEnemy.
{
shortestDistance = distanceToEnemy;
nearestEnemy = enemy;
}
}
if (nearestEnemy != null && shortestDistance <= range) // Sets the target to the nearestEnemy (only if its in range and not null).
{
target = nearestEnemy.transform;
targetEnemy = nearestEnemy.GetComponent<Enemy>();
}
else
{
target = null;
}
}
void Update()
{
if (target == null) // If there is no target, do nothing.
{
if (useLaser)
{
if (lineRenderer.enabled)
{
lineRenderer.enabled = false;
impactEffect.Stop();
impactLight.enabled = false;
audioSource.Stop();
}
}
return;
}
LockOnTarget();
if (useLaser)
{
Laser();
}
else
{
if (fireCountdown <= 0f)
{
Shoot();
fireCountdown = 1f / fireRate;
}
fireCountdown -= Time.deltaTime;
}
}
void Laser()
{
targetEnemy.TakeDamage(damageOverTime * Time.deltaTime);
targetEnemy.Slow(slowAmount);
if (!lineRenderer.enabled)
{
lineRenderer.enabled = true;
impactEffect.Play();
impactLight.enabled = true;
if (audioSource.isPlaying == false)
{
audioSource.Play();
}
}
lineRenderer.SetPosition(0, firePoint.position);
lineRenderer.SetPosition(1, target.position);
Vector3 dir = firePoint.position - target.position;
impactEffect.transform.position = target.position + dir.normalized * 1f;
impactEffect.transform.rotation = Quaternion.LookRotation(dir);
}
void LockOnTarget()
{
Vector3 dir = target.position - transform.position; // Store the direction from turret to target.
Quaternion lookRotation = Quaternion.LookRotation(dir); // I have no idea how this works...
Vector3 rotation = Quaternion.Lerp(partToRotate.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles; // Convert quaternion angles to euler angles and Lerp it (smoothing out the transition).
partToRotate.rotation = Quaternion.Euler(0f, rotation.y, 0f); // Only rotate around the y-axis.
}
void Shoot()
{
UnityEngine.GameObject bulletGO = (UnityEngine.GameObject)Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); // Spawn a bullet at the location and rotation of firePoint. (Also makes this a GO to reference it.
Bullet bullet = bulletGO.GetComponent<Bullet>(); // I have no idea how this works...
audioSource.PlayOneShot(shootingSound, 0.2f);
if (bullet != null) // If there is a bullet, use the seek method from the bullet script.
{
bullet.Seek(target);
}
}
void OnDrawGizmosSelected() // Do this method if gizmo is selected.
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, range); // Draw a wire sphere on the selected point (selected turret f. e.) and give it the towers range.
}
}
The problem was that i had a script called "GameObject" in my Unity Project.
I was also able to rename all "UnityEngine.GameObject" to "Gameobject" by using the Quick Action in Visual Studio.

Unity 2018 - NPC smoothly turning to face the Player while within range

With the current code the NPC will detect the and turn towards the player with the desired animation playing. However, the NPC only snaps to face the player and does not continue to rotate as the player walks around the NPC while they are in range.
I would like to modify this so that the NPC consistently and smoothly turns to face the character while the player is in range of the collider. I figured it would have something to do within void update, but since the function to turn is currently in onTriggerEnter it's a little confusing to a beginner such as myself.
public class helloTrigger : MonoBehaviour
{
public Animator anim;
public CharacterController player;
public Transform Player;
void Start()
{
player = GameObject.FindObjectOfType<CharacterController>();
anim = GetComponent<Animator>();
}
void OnTriggerEnter(Collider other)
{
if (other.tag != "Player") return;
anim.Play("Hello");
Vector3 lookAt = Player.position;
lookAt.y = transform.position.y;
transform.LookAt(lookAt);
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
anim.Play("Idle");
}
}
}
If you want a GameObject to face another GameObject smoothly, use Quaternion.LookRotation to calculate the destination rotation then use Quaternion.Lerp to lerp between the current rotation and the destination rotation calculated with Quaternion.LookRotation. Finally, do the lerping over time. See this post to understand how lerping over rotation work. You can do this in the Update function but I will use it in a coroutine function since it gives you more control.
A simple Look-at smoothly function:
IEnumerator LookAtSmoothly(Transform objectToMove, Vector3 worldPosition, float duration)
{
Quaternion currentRot = objectToMove.rotation;
Quaternion newRot = Quaternion.LookRotation(worldPosition -
objectToMove.position, objectToMove.TransformDirection(Vector3.up));
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
objectToMove.rotation =
Quaternion.Lerp(currentRot, newRot, counter / duration);
yield return null;
}
}
Your new script:
public class helloTrigger : MonoBehaviour
{
public Animator anim;
public CharacterController player;
public Transform Player;
Coroutine smoothMove = null;
// Use this for initialization
void Start()
{
player = GameObject.FindObjectOfType<CharacterController>();
anim = GetComponent<Animator>();
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
anim.Play("Hello");
LookSmoothly();
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
anim.Play("Idle");
}
}
void LookSmoothly()
{
float time = 1f;
Vector3 lookAt = Player.position;
lookAt.y = transform.position.y;
//Start new look-at coroutine
if (smoothMove == null)
smoothMove = StartCoroutine(LookAtSmoothly(transform, lookAt, time));
else
{
//Stop old one then start new one
StopCoroutine(smoothMove);
smoothMove = StartCoroutine(LookAtSmoothly(transform, lookAt, time));
}
}
IEnumerator LookAtSmoothly(Transform objectToMove, Vector3 worldPosition, float duration)
{
Quaternion currentRot = objectToMove.rotation;
Quaternion newRot = Quaternion.LookRotation(worldPosition -
objectToMove.position, objectToMove.TransformDirection(Vector3.up));
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
objectToMove.rotation =
Quaternion.Lerp(currentRot, newRot, counter / duration);
yield return null;
}
}
}

child transform does not move when moving the parent transform

I have a player controlled plattform. When moving to the outer edge, the platform starts to move. The width of the outer triggers are calculated by code.
So the player can move to any direction, he just needs to stay near the edge to trigger the movement.
The player got a Rigidbody attached, the platform too. Here is an image of the inspector of platform I use
and this is the code attached
[SerializeField]
private float speed; // the movementSpeed
[SerializeField]
private float movementTriggerWidth; // width of the triggers at the outer edges
private Vector3 movementDirection = Vector3.zero;
private Rigidbody platformRigid;
private GameObject player;
private float triggerDistance; // distance from center to a trigger
private void Start()
{
player = Globals.GetPlayerObject(); // search for the player Object in the scene
platformRigid = GetComponent<Rigidbody>();
triggerDistance = transform.localScale.x / 2 - movementTriggerWidth; // set the trigger distance
}
private void OnTriggerEnter(Collider col)
{
col.transform.parent = transform; // set the player as a child of the platform
}
private void OnTriggerExit(Collider col)
{
col.transform.parent = null; // leave the platform
}
private void OnTriggerStay(Collider col)
{
if (col.gameObject == player) // only the player can move the platform
{
Vector3 playerPosition = player.transform.position;
Vector3 platformPosition = transform.position;
if (Vector3.Distance(playerPosition, platformPosition) > triggerDistance) // player is in outer trigger?
{
movementDirection = playerPosition - platformPosition; // calculate the movement direction
platformRigid.MovePosition(transform.position + movementDirection * speed * Time.deltaTime); // move the platform
}
}
}
Now the problem:
When jumping on a platform, the player becomes a child of the platform. But when the platform starts moving the player is not affected by this. He doesn't get moved by the platform.
I hope someone can help me solving this "little" ( ? ) bug.
Update:
Here is a picture of the player inspector
Well I went for some tries and this is a working solution for me.
I removed the rigidbody
and took this code
[SerializeField]
private float speed;
[SerializeField]
private float movementTriggerWidth;
private Vector3 movementDirection;
private bool move = false;
private GameObject player;
private float triggerDistance;
private void Start()
{
player = Globals.GetPlayerObject();
triggerDistance = transform.localScale.x / 2 - movementTriggerWidth;
}
private void OnTriggerEnter(Collider col)
{
col.transform.parent = transform;
}
private void OnTriggerExit(Collider col)
{
col.transform.parent = null;
}
private void OnTriggerStay(Collider col)
{
if (col.gameObject == player)
{
Vector3 playerPosition = player.transform.position;
Vector3 platformPosition = transform.position;
if (Vector3.Distance(playerPosition, platformPosition) > triggerDistance)
{
movementDirection = playerPosition - platformPosition;
move = true;
}
}
}
private void FixedUpdate()
{
if (move)
transform.Translate(movementDirection * speed * Time.deltaTime);
move = false;
}
Now the platform is moved by Translate() in the FixedUpdate(). OnTriggerStay() just checks, if the platform should be moved or not.

Npc animation following player unity 2d top down

Hey I am making a top down game in unity. The problem I ma having is making npc players change the way they are facing while following the player. So if the player turns left the npc follows them but doesn't turn to face the direction the npc is going. I can get the npc to look like its walking just not change the direction it is looking. This is a 2d top down game please any help will be nice. here is my npc code.
using UnityEngine;
using System.Collections;
public class SlimeController : MonoBehaviour
{
public Transform Character; // Target Object to follow
public float speed = 0.1F; // Enemy speed
public float maxDist = 10.0f;
public float attackdistance = 3;
public float farenough;
private Vector3 directionOfCharacter;
private bool challenged = false;// If the enemy is Challenged to follow by the player
public Transform StartMarker;
private Vector3 goback;
public Transform EndMarker;
public Rigidbody2D rb;
Animator anim;
float oldx;
bool left;
bool right;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
anim= GetComponent<Animator>();
oldx = transform.position.x;
}
void Update()
{
anim.SetBool("left", false);
anim.SetBool("right", false);
var distanceFromPlayer = Vector3.Distance(Character.position, transform.position);
if(oldx>transform.position.x)
{
left = false;
right = true;
}
if(oldx<transform.position.x)
{
left = true;
right = false;
}
if (oldx == transform.position.x)
{
left = false;
right = false;
}
if (challenged)
{
directionOfCharacter = Character.transform.position - transform.position;
directionOfCharacter = directionOfCharacter.normalized; // Get Direction to Move Towardsss
transform.Translate(directionOfCharacter * speed, Space.World);
enabled = true;
if (distanceFromPlayer < attackdistance)
{
attack();
}
if (distanceFromPlayer > attackdistance)
{
speed = 0.03f;
}
}
if (!challenged)
{
goback = StartMarker.transform.position - transform.position;
goback = goback.normalized;
transform.Translate(goback * speed, Space.World);
}
}
// Will be triggered as soon as player would touch the Enemy Object
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.name == ("Player"))
{
challenged = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.name == ("Player"))
{
speed = 0.03f;
challenged = false;
}
}
void attack()
{
speed = 0;
transform.Translate(directionOfCharacter * speed, Space.World);
}
}
This is because you are just moving the object towards your target. But to have it look at your target you need to also rotate it in the direction of your target.
The Transform Component has a function called LookAt. You supply it with your Target and the Axis your object should rotate around. So in your case:
this.transform.LookAt(Character, Vector3.up);
See here for more info on LookAt.

Object following Player

I am messing around in Unity and wanted to make a mechanic where a box would touch another object and then that object would follow the Player.
I have Cube set up like this:
And a Sphere with a Box Collider with same options.
My Player script is thus:
public class Player : MonoBehaviour {
public float speed = 0.0f;
public float moveX = 0.0f;
public float moveY = 0.0f;
public GameObject player;
public GameObject obj;
//public float force = 0.0f;
private bool collided = false;
// Use this for initialization
void Start () {
player = GameObject.FindWithTag ("Player");
}
// Update is called once per frame
void FixedUpdate () {
moveX = Input.GetAxis ("Horizontal");
moveY = Input.GetAxis ("Vertical");
player.GetComponent<Rigidbody> ().velocity = new Vector2 (moveX * speed, moveY * speed);
}
void OnCollisionEnter (Collision col) {
if (col.gameObject == obj) {
collided = true;
}
}
void OnCollisionExit (Collision col) {
if (col.gameObject == obj) {
collided = false;
}
}
void Update () {
if(collided) {
obj.transform.position = (player.transform.position - obj.transform.position)*speed;
}
}
}
What have I yet to do? Hoping someone can nudge me in the right direction.
I will provide you two scripts.
1st Script is FollowTarget. This will follow your target forcely.
2nd Script is SmoothFollow which will follow your target in a smooth movement.
FollowTarget.cs
using System;
using UnityEngine;
public class FollowTarget : MonoBehaviour
{
public Transform target;
public Vector3 offset = new Vector3(0f, 7.5f, 0f);
private void LateUpdate()
{
transform.position = target.position + offset;
}
}
SmoothFollow.cs
using UnityEngine;
public class SmoothFollow : MonoBehaviour
{
// The target we are following
[SerializeField]
private Transform target;
// The distance in the x-z plane to the target
[SerializeField]
private float distance = 10.0f;
// the height we want the camera to be above the target
[SerializeField]
private float height = 5.0f;
[SerializeField]
private float rotationDamping;
[SerializeField]
private float heightDamping;
// Use this for initialization
void Start() { }
// Update is called once per frame
void LateUpdate()
{
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position = new Vector3(transform.position.x ,currentHeight , transform.position.z);
// Always look at the target
transform.LookAt(target);
}
}
Just choose one of them and then attach it to the gameObject. Like another box that is suppose to follow.
Remove your Update() function in your script as you don't need it anymore.
Also Remove your OnCollisionExit()
void OnCollisionEnter (Collision col) {
if (col.gameObject == obj) {
// If you choose to use SmoothFollow Uncomment this.
//col.GetComponent<SmoothFollow>().target = this.transform;
// If you choose to use FollowTarget Uncomment this
//col.GetComponent<FollowTarget>().target = this.transform;
}
}

Categories

Resources