I'm stuck on enemy 2D AI code in this tutorial

Viewed 27

I was watching this confusing tutorial about how to make a simple AI that follows the player object. when doing that on the video, it tells me to make this 'castPoint' variable thing that has not been defined. I know... I KNOW it sounds really simple but this the tutorial with the guy is so slow into detail which makes it hard for me who has dyslexia to focus and understand well. I'm not the best at C# too so yeah. so please somebody help me!

Here is the code, video, were I was @t:

Part 1:https://youtu.be/nEYA3hzZHJ0

Part 2(were I left off (15:05) i think not so sure ): https://www.youtube.com/watch?v=2VX8uD_xUlM&list=PLnDBHcU45HjxLrObJRO-V_eiTOmJs0ij0&index=4&t=264s

my code :

`using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Presentsaswehear : MonoBehaviour
{
    // Start is called before the first frame update
    //ignore this code!!!
    private Animator Enemtestone;
    public GameObject FirstTrig;
    public static bool Izintyloip;
    private bool Thecheckert;
    //ignore this code!!!


    //Enemy part tracking blablablaaaaa
    [SerializeField]
    public Transform Player;

    [SerializeField]
    float agroRange;

    [SerializeField]
    float moveSpeed;

   private  Rigidbody2D rb2det;  
    //End of ai part

    

    void Start()
    {
       //ignore this code!!!
       Enemtestone = GetComponent<Animator>();
        //FirstTrig.SetActive(true);
        Izintyloip = false;
        
        Thecheckert = false;
        //ignore this code!!!
        
        rb2det = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Izintyloip == true) { 
            Enemtestone.SetInteger("Wakeup", 1);
            Thecheckert = true;
        }
        if (CanSeePlayer(agroRange))
        {

            ChasePlayer();

        }
        else
        {

            StopChasingPlayer();

        }
        
        
        
        
        
        
        
        
        
        /*if (Thecheckert == true)
        {
            float distToPlayer = Vector2.Distance(transform.position, Player.position);
            if (distToPlayer < agroRange)
            {

                ChasePlayer();

            }
            else
            {

                StopChasingPlayer();

            }



        }*/
    }

    
    bool CanSeePlayer(float distance)
    {
        bool val = false;
        float castDist = distance;
        
        Vector2 endPos = castPoint.position + Vector3.right * distance;
        
        RaycastHit2D hit = Physics2D.Linecast(castPoint.position, endPos, 1 << LayerMask.NameToLayer("Action"));
        if(hit.collider != null)
        {

            if (hit.collider.gameObject.CompareTag("Player"))
            {
                val = true;

            }
            else
            {
                val = false;
            }


        }
        return val;
    }

    void ChasePlayer()
    {

        if(transform.position.x < Player.position.x)
        {

            rb2det.velocity = new Vector2(moveSpeed, 0);
            transform.eulerAngles = new Vector3(0, 180, 0);
        }
        else
        {

            rb2det.velocity = new Vector2(-moveSpeed, 0);

            transform.eulerAngles = new Vector3(0, 0, 0);

            // if(transform.position.x > player.postion.x)
        }

        Enemtestone.SetBool("GFW", true);
    }
    void StopChasingPlayer()
    {
        rb2det.velocity = new Vector2(0, 0);

        Enemtestone.SetBool("GFW", false);
    }
}
`
1 Answers

You've missed the step at 7:49 where he adds the transform called castPoint.

Related