在C#中创建分散行为

时间:2022-11-11 16:13:44

Hi I am practising my coding for my university course and I have built a virtual Aquarium.

嗨,我正在练习我的大学课程编码,我已经建立了一个虚拟水族馆。

I have currently got some sea horses in there and have made a chicken leg to appear when I click. I have written some code to say when the chicken leg appears they move vertically. However I want them to scatter and not go near the chicken leg or at least bump of it. my vertical swim behaviour code is:

我现在有一些海马在我点击时出现了一条鸡腿。我写了一些代码,说当鸡腿出现时它们会垂直移动。然而,我希望它们散开,而不是靠近鸡腿或至少碰到它。我的垂直游泳行为代码是:

` private void VerticalSwimBehaviour() //Verticle Swim scatter
    {
        Vector3 tokenPosition = this.PossessedToken.Position;
        tokenPosition.Y = tokenPosition.Y + mSpeed * mFacingDirection;

        if (tokenPosition.Y > 300 || tokenPosition.Y < -300) // Responds if fish goes to the top or bottom of screen
        {
            mFacingDirection = -mFacingDirection;
        }
        this.PossessedToken.Orientation = new Vector3(mFacingDirection, this.PossessedToken.Orientation.Y, this.PossessedToken.Orientation.X);
        this.PossessedToken.Position = tokenPosition;
    }
    private void RandomNumberMethod()
    {
        Random random = new Random();
        int randomNumber1 = random.Next(1, 5);
        int randomNumber2 = random.Next(1, 150);
    }`

I want to make a behaviour that work like this but runs from the chicken leg.

我想做一个像这样工作但从鸡腿跑的行为。

here is my leg calling upon the behaviour when chicken leg appears

这是我的腿呼唤鸡腿出现时的行为

public override void Update(ref GameTime pGameTime)
    {
        Vector3 tokenPosition = this.PossessedToken.Position;
        HorizontalSwimBehaviour();
        velocidadMax();

        if (mAquarium.ChickenLeg != null)
        {
            VerticalSwimBehaviour();
        }



    }

Any help is appreciated.

任何帮助表示赞赏。

1 个解决方案

#1


Write a method to check if the seahorses are facing the chicken. If they are, reverse their facing (do this just once). Have them keep a certain distance away from the chicken. Use the distance formula with the origin of the chicken and the origin of each seahorse. If they are within that distance have them run straight in one direction until they hit a wall. When they hit the wall, have them turn 90 degrees right or left and do it again. Do this behavior until they are far enough away from the chicken. I would probably make the distance about 40% of the tank size (as a diameter) to ensure that they can find a corner to hide. Hopefully this helps, I haven't used Unity so I don't know specific methods to call. It should be simple object manipulation though. I would also add some effects to your vertical oscillation, perhaps increasing its frequency with a multiplier based on closeness to the chicken (lower oscilation distance, higher velocity). As for avoiding the running in the the chicken, You can set a minimum distance to the chicken (this one much closer) which will cause your seahorse to turn around if it gets too close, in which case it will resume its running to a wall and turning. All of this checking should run while the chicken leg exists.

写一个方法来检查海马是否面向鸡。如果他们是,扭转他们的面孔(只做一次)。让它们与鸡保持一定距离。使用距离公式与鸡的起源和每只海马的起源。如果它们在那个距离内,它们会朝一个方向直行,直到它们碰到一堵墙。当他们撞到墙壁时,让它们向右或向左转90度并再次进行。做这种行为,直到它们远离鸡肉。我可能会将距离大约为坦克大小的40%(作为直径),以确保他们可以找到隐藏的角落。希望这有帮助,我没有使用Unity所以我不知道具体的方法来调用。它应该是简单的对象操作。我还会为你的垂直振荡添加一些效果,或许可以通过基于与鸡接近的乘数来增加其频率(较低的振荡距离,较高的速度)。至于避免在鸡肉中跑步,你可以设置与鸡肉的最小距离(这一个更接近),如果它太靠近会导致你的海马转身,在这种情况下,它会恢复运行到墙上转身。所有这些检查都应该在鸡腿存在时进行。

#1


Write a method to check if the seahorses are facing the chicken. If they are, reverse their facing (do this just once). Have them keep a certain distance away from the chicken. Use the distance formula with the origin of the chicken and the origin of each seahorse. If they are within that distance have them run straight in one direction until they hit a wall. When they hit the wall, have them turn 90 degrees right or left and do it again. Do this behavior until they are far enough away from the chicken. I would probably make the distance about 40% of the tank size (as a diameter) to ensure that they can find a corner to hide. Hopefully this helps, I haven't used Unity so I don't know specific methods to call. It should be simple object manipulation though. I would also add some effects to your vertical oscillation, perhaps increasing its frequency with a multiplier based on closeness to the chicken (lower oscilation distance, higher velocity). As for avoiding the running in the the chicken, You can set a minimum distance to the chicken (this one much closer) which will cause your seahorse to turn around if it gets too close, in which case it will resume its running to a wall and turning. All of this checking should run while the chicken leg exists.

写一个方法来检查海马是否面向鸡。如果他们是,扭转他们的面孔(只做一次)。让它们与鸡保持一定距离。使用距离公式与鸡的起源和每只海马的起源。如果它们在那个距离内,它们会朝一个方向直行,直到它们碰到一堵墙。当他们撞到墙壁时,让它们向右或向左转90度并再次进行。做这种行为,直到它们远离鸡肉。我可能会将距离大约为坦克大小的40%(作为直径),以确保他们可以找到隐藏的角落。希望这有帮助,我没有使用Unity所以我不知道具体的方法来调用。它应该是简单的对象操作。我还会为你的垂直振荡添加一些效果,或许可以通过基于与鸡接近的乘数来增加其频率(较低的振荡距离,较高的速度)。至于避免在鸡肉中跑步,你可以设置与鸡肉的最小距离(这一个更接近),如果它太靠近会导致你的海马转身,在这种情况下,它会恢复运行到墙上转身。所有这些检查都应该在鸡腿存在时进行。