Search Unity

Enemy's character controller

Discussion in '2D' started by Redden44, Apr 9, 2015.

  1. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    159
    Hello I'm crating a 2D game for PC and now I'm starting to create my first enemies and I have few doubts maybe you can help me with :)

    I've created a character controller for the player following the 3DBuzz tutorial, which I think it's based on Prime31 character controller; I've changed it a little, added some stuff and it works very well. Basically it's a box collider with several raycasts, from 1 to 4 for the bottom, 0 up to 4 for the top and 0 to 8 for the side.

    In the 3DBuzz tutorial they use the same character controller for a simple enemy and indeed it works fine. I'd like to do the same for my enemies but I 'm not sure if that's a good idea, because the character controller uses from 2 to 13 raycasts to check the collisions..add maybe a couple of raycasts for the AI, multiply that for 10-15 enemies in the scene and that's going to be a lot of raycasting.
    Now I've read quite a few times that raycasting is bad for performances and should be avoided as much as possible; it that really bad for modern pc?

    I think I can keep the raycasting of an enemy pretty low by using a big circular trigger collider2D; if the enemy doesn't move, it casts only a couple of raycast, so I could have him standing still in an idle pose until the player enters the trigger and activate the enemy. Also I could reduce the number of the raycasts on the vertical side from 8 to maybe 4, which will decrease the maximum number of raycasts to less than 10 I guess.
    Could this work or the number of raycasts would be a problem still (with 10-15 enemies in the scene)?

    I could use the physics engine to handle the enemy's movement but I don't want to though, because it changes the movement speed on slopes and I don't want that; also if I use a box collider sometimes it gets stuck between the gap of two ground colliders, so I have to use sphere colliders at the base or polygon/edge colliders but then I have problems on the edges of platforms..so it's a little messy.

    What do you think? Is the raycasting going to be fine on modern pc? How can I check that? Should I just run the game and look for a fps drop? Do you have any tips for an alternative solution?

    Thanks :)
     
  2. Brainswitch

    Brainswitch

    Joined:
    Apr 24, 2013
    Posts:
    270
    You can get away with thousands of raycasts per frame on a modern computer without much of any performance issues.

    If you really think the raycasts are/will be a performance hog, just multiply the amount of enemies with say 10 and then run your game.

    My current sideproject involves raycasting character controllers, but I cast up to 240 raycasts per character (it's in 3D, so I require a lot more raycasts) per FixedUpdate. While I could optimize that down quite a bit, there has been no need for it.
    Watch out for premature optimizations!
     
    Last edited: Apr 9, 2015
    theANMATOR2b likes this.