I want to make shells. I added everything, turned to my Ammo code to put the location of the projectile departure. There are no errors in the c# code itself, but Unity complains.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour
{
private float cooldownTimer = Mathf.Infinity;
[SerializeField] private float attackCooldown;
[SerializeField] private Transform firePoint;
[SerializeField] private GameObject[] fireballs;
private Animator anim;
private PlayerControllert playerMovement;
private void Awake()
{
anim = GetComponent<Animator>();
playerMovement = GetComponent<PlayerControllert>();
}
private void Update()
{
if(Input.GetMouseButtonDown(0) && cooldownTimer > attackCooldown)
{
Attack();
}
cooldownTimer += Time.deltaTime;
}
private void Attack()
{
anim.SetTrigger("attack");
cooldownTimer = 0;
fireballs[0].transform.position = firePoint.position;
fireballs[0].GetComponent<Ammo>().SetDirection(Mathf.Sign(transform.localScale.x));
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ammo : MonoBehaviour
{
[SerializeField] private float speed;
private bool hit;
private Animator anim;
private BoxCollider2D boxCollider;
private float direction;
private void Awake()
{
anim.GetComponent<Animator>();
boxCollider = GetComponent<BoxCollider2D>();
}
private void Update()
{
if (hit) return;
float movementSpeed = speed * Time.deltaTime * direction;
transform.Translate(movementSpeed, 0, 0);
}
private void OnTriggerEnter2D(Collider2D collision)
{
hit = true;
boxCollider.enabled = false;
anim.SetTrigger("explode");
}
public void SetDirection(float _direction)
{
direction = _direction;
gameObject.SetActive(true);
hit = false;
boxCollider.enabled = true;
float localScaleX = transform.localScale.x;
if (Mathf.Sign(localScaleX) != _direction)
localScaleX = -localScaleX;
transform.localScale = new Vector3(localScaleX, transform.localScale.y, transform.localScale.z);
}
private void Deactivate()
{
gameObject.SetActive(false);
}
}
Ammo Player Attack I repeated everything according to the guide. I've watched the video several times, but for some reason it still doesn't work for me