C
C#10mo ago
Hex_Tv

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