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