c# – How to make Enemy Wander in 2d topdown unity

Im making a top down shooter in unity. The enemy ai travels straight to the middle before actually wandering when I put in the code. What should I do to fix that?
I copy the code in this video:https://www.youtube.com/watch?v=FdNervYWmcE

using System.Collections.Generic;
using UnityEngine;

public class EnemyWander : MonoBehaviour
{
    (SerializeField)
    float speed;
    (SerializeField)
    float range;
    (SerializeField)
    float maxDis;
    Vector2 Waypoint;
    // Start is called before the first frame update
    void Start()
    {
        setNewDes();
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Vector2.MoveTowards(transform.position, Waypoint, speed * Time.deltaTime);
        if(Vector2.Distance(transform.position,Waypoint) < range)
        {
            setNewDes();
        }
    }
    void setNewDes()
    {
        Waypoint = new Vector2(Random.Range(-maxDis, maxDis), Random.Range(-maxDis, maxDis));
    }
}

c# – Wandering Enemy in unity

Im making a top down shooter in unity. The enemy ai travels straight to the middle before actually wandering when I put in the code. What should I do to fix that?
I copy the code in this video:https://www.youtube.com/watch?v=FdNervYWmcE

using System.Collections.Generic;
using UnityEngine;

public class EnemyWander : MonoBehaviour
{
    (SerializeField)
    float speed;
    (SerializeField)
    float range;
    (SerializeField)
    float maxDis;
    Vector2 Waypoint;
    // Start is called before the first frame update
    void Start()
    {
        setNewDes();
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Vector2.MoveTowards(transform.position, Waypoint, speed * Time.deltaTime);
        if(Vector2.Distance(transform.position,Waypoint) < range)
        {
            setNewDes();
        }
    }
    void setNewDes()
    {
        Waypoint = new Vector2(Random.Range(-maxDis, maxDis), Random.Range(-maxDis, maxDis));
    }
}

python – Simple enemy movement simulation on Tkinter

Kind of a noob at Python here, and this is one of my first “big”(big for a beginner like me) project that I undertook with Tkinter and Pynput. Basically, this code will simulate an enemy’s movement pattern based on some conditions that I made(you will be able to see the different “phases” being printed out on the console). You can then control the player using the arrow keys.

I would like some advice on what I should improve on for future projects. Should I add more comments? Is the code structured well? etc.

import math
import tkinter as tk
from pynput import keyboard
class Application:
    def __init__(self, master, height = 800, width = 800, updatesPerSecond = 10, safeCircle = True):
        self.height = height
        self.width = width
        self.root = master
        self.updatesPerSecond = updatesPerSecond
        self.player = Player()
        self.enemy = Enemy()
        self.safeCircle = safeCircle
        self.canvas = tk.Canvas(self.root, height = self.height, width = self.width)
        self.canvas.pack()
        self.player_rectangle = self.canvas.create_rectangle(self.player.x-self.player.hLength, self.player.y-self.player.hLength, self.player.x+self.player.hLength, self.player.y+self.player.hLength)
        self.enemy_rectangle = self.canvas.create_rectangle(self.enemy.x-self.player.hLength, self.enemy.y-self.player.hLength, self.enemy.x+self.player.hLength, self.enemy.y+self.player.hLength)
        if self.safeCircle:
            self.safe_circle = self.canvas.create_oval(self.player.x-self.enemy.safe_distance, self.player.y-self.enemy.safe_distance, self.player.x+self.enemy.safe_distance, self.player.y+self.enemy.safe_distance)
        self.keypress_list = ()
        self.listener = keyboard.Listener(on_press = self.on_press, on_release = self.on_release)
        self.listener.start()
        self.player_movement()
        self.enemy_movement()
    def player_movement(self):
        if "down" in self.keypress_list:
            self.player.update_y(self.player.speed)
        if "up" in self.keypress_list:
            self.player.update_y(-self.player.speed)
        if "left" in self.keypress_list:
            self.player.update_x(-self.player.speed)
        if "right" in self.keypress_list:
            self.player.update_x(self.player.speed)
        self.player.boundary_check(self.height, self.width)
        self.canvas.coords(self.player_rectangle, self.player.x-self.player.hLength, self.player.y-self.player.hLength, self.player.x+self.player.hLength, self.player.y+self.player.hLength)
        if self.safeCircle:
            self.canvas.coords(self.safe_circle, self.player.x-self.enemy.safe_distance, self.player.y-self.enemy.safe_distance, self.player.x+self.enemy.safe_distance, self.player.y+self.enemy.safe_distance)
        self.root.after(1000//self.updatesPerSecond, self.player_movement)
    def enemy_movement(self):
        self.enemy.update_pos(self.player)
        self.enemy.boundary_check(self.height, self.width)
        self.canvas.coords(self.enemy_rectangle, self.enemy.x-self.enemy.length/2, self.enemy.y-self.enemy.length/2, self.enemy.x+self.enemy.length/2, self.enemy.y+self.enemy.length/2)
        self.root.after(1000//self.updatesPerSecond, self.enemy_movement)
    def key_test(self, key):
        try:
            return key.name
        except:
            return
    def on_press(self, key):
        key = self.key_test(key)
        if not key in self.keypress_list:
            self.keypress_list.append(key)
    def on_release(self, key):
        key = self.key_test(key)
        self.keypress_list.remove(key)

class SimObject:
    def __init__(self, x, y, speed, length):
        self.x = x
        self.y = y
        self.speed = speed
        self.length = length
        self.hLength = self.length/2
    def boundary_check(self, height, width):
        if self.x - self.hLength < 0:
            self.x = self.hLength
        if self.y - self.hLength < 0:
            self.y = self.hLength
        if self.x + self.hLength > width:
            self.x = width - self.hLength
        if self.y + self.hLength > height:
            self.y = height - self.hLength
    def update_x(self, offset):
        self.x+=offset
    def update_y(self, offset):
        self.y+=offset

class Player(SimObject):
    def __init__(self, x = 400, y = 400, speed = 10, length = 20):
        super().__init__(x, y, speed, length)

class Enemy(SimObject):
    def __init__(self, x = 10, y = 10, speed = 5, length = 20, safe_distance = 100):
        super().__init__(x, y, speed, length)
        self.safe_distance = safe_distance
        self.last_phase = -1
    def update_phase(self, n):
        phase_list=(f"{i} Phase" for i in ("Orbit", "Rush", "Run"))
        if self.last_phase!=n:
            print(phase_list(n))
            self.last_phase = n
    def update_pos(self, player):
        PI=math.pi
        dx=player.x-self.x
        dy=player.y-self.y
        g_to_p_ang=math.atan2(dy,dx)
        p_to_g_ang=PI+g_to_p_ang
        dist=math.sqrt(dx*dx+dy*dy)
        ang_increase=self.speed/self.safe_distance
        t=p_to_g_ang
        if abs(dist-self.safe_distance)<=self.speed:#near the orbit
            self.update_phase(0)
            t+=ang_increase
            self.x=self.safe_distance*math.cos(t)+player.x
            self.y=self.safe_distance*math.sin(t)+player.y
        elif dist>self.safe_distance:#far from orbit
            self.update_phase(1)
            self.update_x(self.speed*math.cos(g_to_p_ang))
            self.update_y(self.speed*math.sin(g_to_p_ang))
        elif dist<self.safe_distance:#far inside of orbit
            self.update_phase(2)
            self.update_x(self.speed*math.cos(p_to_g_ang))
            self.update_y(self.speed*math.sin(p_to_g_ang))


root = tk.Tk()
root.resizable(0,0)
root.title("Enemy Movement Test")
application = Application(root)
tk.mainloop()

dnd 5e – Does Know Your Enemy provide all class levels or just fighter levels?

Distinct Fighter levels and Unknowable Class Levels

As per the wording of the ability:

The DM tells you if the creature is your equal, superior, or inferior in regard to two of the following characteristics of your choice:

  • Strength score
  • Dexterity score
  • Constitution score
  • Armor Class
  • Current hit points
  • Total class levels, if any
  • Fighter class levels, if any

So, for the two things the fighter can learn, total class levels and total fighter levels are valid options to learn. But this does NOT tell you the total levels in other classes.

E.g. If you asked about the total level of a NPC and the fighter levels you might get back the answer “higher” for total levels and “lower” for fighter levels without being told what those extra class levels are.

Also, remember that the DM only has to tell you if they are superior, equal or inferior, in the chosen criteria, compared to the fighter, not what that actual value would be.

unity – Unwated delay in player controller FixedUpdate compared to enemy

I’m making a mobile game that uses rigidbodies to move the player and enemies. I am applying forces to those rigidbodies on ever fixed update cycle.

To handle touches, I have created a TouchManager script. The game UI (and the joystick) is handled by a GameUI script that listens to the touches of the touch managing script. Then, the PlayerController script listens to any updates to the joystick from the game ui script, and converts changes to the joystick to actual movement.

This has created a problem where there is a consistent cycle delay (about 2-4 cycles) between input and execution every few cycles (FixedUpdate cycles).

For example: input will be processed on cycle 1, but movement inside FixedUpdate will be called for cycles 3, 4, 5. Then, no movement until again cycles 8, 9, 10 and so on.
This gap in cycles is the problem. The intention is for input to be processed on cycle 1, and at the latest, on cycle 2, movement be applied to the rigidbody without skipping cycles (2, 3, 4, 5, 6 and so on without any gaps, until there is no input).

Below, you can see my code separated in individual scripts.
Note: TouchData is a custom structure that stores all the information a touch provides (initial and latest touch position on screen).

TouchManager

using System;
using UnityEngine.EventSystems;

public class TouchManager : MonoBehaviour {
    public static event Action<TouchData> OnTouch = delegate{};
    public static event Action<TouchData> OnTouchEnd = delegate{};
    public static List<TouchData> Touches = new List<TouchData>();
    void Update() {
        TouchData data;
        foreach(Touch touch in Input.touches) {
            if (touch.phase == TouchPhase.Began) {
                data = new TouchData(){
                    TouchID = touch.fingerId,
                    StartPoint = touch.position,
                    EndPoint = touch.position,
                    touchedUI = CheckUITouch(touch.fingerId),
                };
                Touches.Add(data);
                OnTouch(data); //GameUI uses this information
            } else if ... //the rest of the phases are processed in a similar fashion
        }
    }
}

GameUI

using System;
using UnityEngine;
using UnityEngine.UI;

public class GameUI : MonoBehaviour {
    public static event Action<Vector3> JoystickMoved = delegate{};
    void Start() {
        TouchManager.OnTouch += HandleTouch;
    }

    public void HandleTouch(TouchData touchData) {
        HandleJoystick(touchData);
    }

    public void HandleJoystick(TouchData touchData) {
        //several checks to see if the current touch is the correct
        //touch related with joystick movement
        if (joystickTouchID == touchData.TouchID) {
            //knobPos is a Vector3 that is the direction of the force applied to the
            //player rigidbody in the next script
            JoystickMoved(knobPos); //PlayerController then uses this information
        }
    }
}

PlayerController

using UnityEngine;
using System;

public class PlayerController : MonoBehaviour {
    void Awake() {
        GameUI.JoystickMoved += MovePlayer;
    }

    void FixedUpdate() {
        _rigidbody.AddForce(moveTarget * moveSpeed * Time.deltaTime, ForceMode2D.Impulse);
        moveTarget = Vector3.zero;
    }

    private void MovePlayer(Vector3 pos) {
        moveTarget = pos.normalized;
        LookDirection(pos);
    }
}

Am I right to assume that this way of doing things, is causing my player to “lose” fixed update cycles and present with gaps in the log?

If using event Action is indeed causing the cycle gaps, how could I solve this without processing all touches inside the player controller? In other words, what is an elegant way of solving the cycle gap problem I am facing?

Thank you for your time.

dnd 5e – Are there any consequences to walking into a hidden enemy?

Suppose a PC is looking at an area of darkness that a foe has Hidden in. The PC would prefer not to guess the square the enemy is in and potentially waste their attack, so they decide to use their movement to physically traverse the area and see if they ‘bump into’ the enemy – if they succeed they could then attack the correct square.

This all seems to be possible within the rules – but are there any consequences to blindly bumbling into such an enemy (such as allowing the enemy a reaction attack)? Does the answer change if the enemy can see in the dark?

dnd 5e – If an attack through an Echo Knight’s echo hits a paralyzed enemy , is the attack automatically a critical hit?

The Echo Knight can create an echo of itself, which can then be used to attack:

When you take the Attack action on your turn, any attack you make with that action can originate from your space or the echo’s space. You make this choice for each attack.

Suppose an attack originating from the echo hits a paralyzed creature. The Paralyzed condition states the following:

Any Attack that hits the creature is a critical hit if the attacker is within 5 feet of the creature.

If the echo is within 5 feet of the target but the Echo Knight isn’t, is the attack a critical hit?

dnd 5e – What do I do when the enemy has an AC higher than 20?

ACs don’t get that high.

In the DMG (pg. 274), there’s a big table that lists offensive and defensive stats by CR. In that table, the listed AC for a CR 30 monster is only 19.

The Tarrasque, the highest CR monster in the game, has an AC of only 25. The runners up, the Ancient Gold and Red Dragons, have ACs of 22. Hence, it’s very unlikely that you’ll run up against an AC of 30, even at high levels.

Bonuses get you there

As you level up, you’ll accumulate abilities that will give you a bonus to your attack rolls, such as higher ability scores and proficiency, as well as things like Bardic Inspiration or magic items. All of these things add flat values to your dice roll, and thus make hitting ACs over 20 feasible.

For example, 1st level fighter, with a strength of 16 (+3) and a proficiency bonus of +2, has a +5 to hit. If she rolls a 17 on the die, she adds 5 to it, making a total attack roll value of 22. That means that she only needs to roll a 17 or higher to hit an Ancient Red Dragon, so she has a 20% chance to hit without any other bonuses. Of course, dealing enough damage to be more than an annoyance is another story.

dnd 5e – If my Arcane Trickster’s owl familiar is within 5 ft of the enemy, can it both give advantage to the next attacker and trigger Sneak Attack?

My character is an Arcane Trickster and I’m trying to figure out what his owl familiar can and can’t do.

I know that if the owl is within 5 feet of the target, it can use the Help action on its turn to aid the target’s next attacker:

You [the owl] can aid a friendly creature in attacking a creature within 5 feet of you. You feint, distract the target, or in some other way team up to make your ally’s Attack more effective. If your ally attacks the target before your next turn, the first Attack roll is made with advantage.

I also know that when it’s my turn, if the owl is within 5 feet of the target, I can use Sneak Attack:

You [a Rogue] don’t need advantage on the Attack roll if another enemy of the target is within 5 feet of it, that enemy isn’t Incapacitated, and you don’t have disadvantage on the Attack roll.

So let’s say I’m adventuring with a character named Bob, and we’re in combat, and the order is owl > Bob > me.

Can the owl, since it is within 5 feet of the target,

  1. use the Help action to give Bob advantage on his attack AND
  2. allow me to use Sneak Attack?

If so, can the owl still use its flyby ability to avoid opportunity attacks?