Alright, so I’m working on an aim assist system, and the hurdle I’m running into is being unable to have the transform from a list of enemies in a box collider zone (which is a child to the camera). Essentially what I’m trying to do is aim a weapon (also a child to the camera) to aim at the closest enemy in the zone. I’m fairly certain I can figure out how to rotate the weapon to the nearest enemy, I already had debug code set up to to test rotating the weapon independent of the camera, but my problem now is I can’t get the transform of the nearest enemy from the aim assist script in the camera script to aim the weapon towards it. The error I get is detailed at the bottom.
Code for the camera script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraLook : MonoBehaviour
{
[SerializeField] private float mSensX = 8f;
[SerializeField] private float mSensY = 8f;
[SerializeField] Transform playerCamera;
[SerializeField] Transform orientation;
[SerializeField] PlayerBehavior playerBH;
[SerializeField] Transform weapon;
[SerializeField] Transform autoAim; //this is the box collider zone, which has the aim assist script attached to it
float mouseX;
float mouseY;
float mouseMultiplier = 1.4f;
float mRotationX;
float mRotationY;
float rotateAmountX;
float rotateAmountY;
private void Start()
{
//
}
private void Update()
{
//Autoaiming debug code starts here
if (Input.GetKey(KeyCode.L))
{
rotateAmountY += 50 * Time.deltaTime;
}
if (Input.GetKey(KeyCode.J))
{
rotateAmountY += -50 * Time.deltaTime;
}
if (Input.GetKey(KeyCode.K))
{
rotateAmountX += 50 * Time.deltaTime;
}
if (Input.GetKey(KeyCode.I))
{
rotateAmountX += -50 * Time.deltaTime;
}
//and ends here
MouseInput();
playerCamera.transform.localRotation = Quaternion.Euler(mRotationX, mRotationY, playerBH.cameraCurrentTilt);
//weapon.transform.rotation = Quaternion.Euler(mRotationX + rotateAmountX, mRotationY + rotateAmountY, 0); //commented out for now, only worked for debug testing
Quaternion targetDirection = Quaternion.LookRotation(autoAim.closestEnemyInDirection.transform.position - transform.position); //the line where the error appears, I'm not sure if this is even the correct way of getting the results I want, but it's one of a few ways that produces the error. The exact error is written out below.
orientation.transform.rotation = Quaternion.Euler(0, mRotationY, 0);
}
void MouseInput()
{
mouseX = Input.GetAxisRaw("Mouse X");
mouseY = Input.GetAxisRaw("Mouse Y");
mRotationY += mouseX * mSensX * mouseMultiplier;
mRotationX -= mouseY * mSensY * mouseMultiplier;
mRotationX = Mathf.Clamp(mRotationX, -90f, 90f);
}
}
Code for the aim assist script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoAim : MonoBehaviour
{
public List<GameObject> enemies = new List<GameObject>();
public GameObject closestEnemyInDirection;
public Transform body;
Transform closestEnemyTransform;
void Start()
{
}
public void FindClosestGameObject()
{
GameObject closest = null;
float distance = 400f;
Vector3 position = body.transform.position;
foreach (GameObject go in enemies)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
closestEnemyInDirection = closest;
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
for (int i = 0; i < enemies.Count; i++)
{
if (other.gameObject == enemies*)*
{
return;
}
}
enemies.Add(other.gameObject);
FindClosestGameObject();
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == “Enemy”)
{
for (int i = 0; i < enemies.Count; i++)
{
if (other.gameObject == enemies*)*
{
if (enemies == closestEnemyInDirection)
{
closestEnemyInDirection = null;
}
enemies.RemoveAt(i);
}
}
}
}
}
The error I get in the camera script is:
‘Transform’ does not contain a definition for ‘closestEnemyInDirection’ and no accessible extension method ‘closestEnemyDirection’ accepting a first argument of type Transform could be found (are you missng a using directive or an assembly reference?)
Assistance would be appreciated, I’m fairly inexperienced with Unity/C#, and I’m at an impass with this.
Hmm, that is a weird one, I suppose if its a consistent amount you could always just have each bullet be adjusted slightly as soon as its created? so you would just need to use https://docs.unity3d.com/ScriptReference/Transform.Rotate.html
– AaronBacon