© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
45 replies
Hex

I have errors one unity turret script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Turret : MonoBehaviour {

[Header("References")]
[SerializeField] private Transform turretRotationPoint;
[SerializeField] private LayerMask enemyMask;

[Header("Attribute")]
[SerializeField] private float targetingRange = 5f;

private Transform target;

private void Update() {
if (target == null){
FindTarget();
return;
}

RotateTowardsTarget();

if (!CheckTargetIsInRange)(); {
target = null;
}
}

private void FindTarget() {
RaycastHit2D[] hits = Physics2D.CircleCastAll(transform.position, targetingRange, (Vector2)
transform.position, 0f, enemyMask);

if (hits.Length > 0) {
target = hits[0].transform;
}
}

private bool CheckTargetIsInRange(); {
return Vector2.Distance(target.position, transform.position) <= targetingRange;
}

private void RotateTowardsTarget() {
float angle = Mathf.Atan2(target.position.y - transform.position.y, target.position.x -
transform.position.x) * Mathf.Rad2Deg - 90f;

Quaternion targetRotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
turretRotationPoint.rotation = targetRotation;
}

private void OnDrawGizmoSelected() {
Handles.color = Color.cyan;
Handles.DrawWireDisc(transform.position, transform.forward, targetingRange);
}

}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ i have two errors in this code unity Turret code please help
C#CC# / help
3y ago
errors in the script in unity
C#CC# / help
2y ago
I have error in Unity EnemySpawner Script c#
C#CC# / help
3y ago
unity errors
C#CC# / help
7mo ago