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);
}
}
}
26 Replies
Well, what errors are you getting?
alot
also
on
if (!CheckTargetIsInRange)(); {
target = null;
and
private bool CheckTargetIsInRange(); {
return Vector2.Distance(target.position, transform.position) <= targetingRange;
}
Well that's just wrong
Remove the
();
Remove the ;
after ()
ty
i need remove and if (!CheckTargetIsInRange); {
target = null;
!
?
oh, you're not calling the method
if (!CheckTargetIsInRange()) {
Are you using an IDEno
Do that
It will show you errors directly in the code
ok
but i have another problems
in my code
from the start
my turret gun
is crazy xD
do you know why
what?
see my gun
where is he
what
xD
Firstly, that's probably a question better suited for $unity, and secondly that's nowhere near enough details to even understand what the problem is.
my gun instead of spinning around the whole map
Can you actually describe in words what you want to happen and what is happening instead
my gun instead of rotating to where the mobs are he goes around the whole map
like this
and I don't know why it happens like that
Sounds like the pivot is somewhere it shouldn't be