1. For issues you find with the Switch and Console releases, please follow this link and give as much detail as possible. This is the speediest way to get info to Pipeworks and get a hotfix in the works.
    Dismiss Notice

tModLoader I wonder how to make a check collision between blocks and NPCs?

Discussion in 'General Mod Discussion' started by Jefferson Luis Bernardo, Nov 2, 2016.

  1. Code:
    using Terraria;
    using Terraria.ID;
    using Terraria.ModLoader;
    using Terraria.ObjectData;
    using Microsoft.Xna.Framework;
    
    namespace Castleroid.NPCs
    {
        public class gosma : ModNPC
        {
            int contador = 0;
            int segundos = 0;
            public override void SetDefaults()
            {
                npc.name = "Gosma";
                npc.displayName = "Gosma";
                npc.width = 18;
                npc.height = 40;
                npc.damage = 14;
                npc.defense = 6;
                npc.lifeMax = 200;
                npc.soundHit = 7;
                npc.soundKilled = 5;
                npc.noTileCollide = false;//colisões com pisos
                npc.value = 60f;
                npc.knockBackResist = 0f;//1.0f sem resistencia - 0.0000... quanto mais zeros mais resitente
                npc.aiStyle = -1;//Zerada a ai
            }
    
            public override float CanSpawn(NPCSpawnInfo spawnInfo)
            {
                return spawnInfo.spawnTileY < Main.rockLayer && !Main.dayTime ? 0.5f : 0f;
            }
    
            public override void AI()
            {
                npc.collideX = true;
                npc.collideY = true;
                contador += 1;
    
                float vel_x = npc.position.X;
                float vel_y = npc.position.Y;
                if(contador > 60){//60 numero de fps do jogo 60quadros por segundo
                    segundos++;
                    contador = 0;
                }
                    if(segundos > 3){
                        //vel_x += 16;
                        vel_y -= 64;//plano cartesiano o ponto de horigem é diferente - 4 blocos altura
                        segundos = 0;
                    }
                string s =  seconds.ToString();// converte variavel segundos para o formato de texto
                npc.position = new Vector2(vel_x,vel_y);
                Main.NewText(s, 255, 240, 20, false);// Texto com posicao x
               
    
            }
    
        }
    }
     
  2. The npc jumps every 3 seconds, but it does not collide with blocks during the jump
     
  3. Nopezal

    Nopezal Skeletron Prime

    You use 2 variables to manage counting? Pretty wasteful, but it works, I guess...
    Also, you set the position, not the velocity. Change "npc.position = blah" to "npc.velocity = blah"
    Setting the position will ignore tilecollisions because you can set the position inside of a block. Terraria handles tile collisions by checking that position plus the velocity it currently has, and checks if it collides that frame. If it does, it stops the NPC.
     
  4. Thank you for your help...
    Code:
                if(contador > 60){//60 numero de fps do jogo 60quadros por segundo
                    segundos++;
                    contador = 0;
                }
                    if(segundos > 3){
                       npc.velocity.Y = -10;//jump power
                        segundos = 0;
                    }