Unity implements archery game

Target

1. First person, the player can move around and rotate the perspective

2. The arrow has a parabolic trajectory, and players can control the speed of the arrow through different charging times.

3. Use terrain, draw grass and trees, trees have collision volumes

4. There are stationary targets and moving targets. The moving targets use Unity’s animation system to control the movement trajectory and speed.

5. There is a shooting area, and firing can only be carried out within the shooting area.

6. Use different skyboxes in different areas

7. Score, score points by hitting the target

Resources used: Standard Assets (using the first-person movement controller), Fantasy Skybox FREE (skybox, terrain texture, grass texture), Free trees (tree model), Crossbow (crossbow and arrows)
1. Realize the player’s movement and rotation of perspective

By organizing resources in the following order, you can achieve movement and perspective control, and at the same time make the crossbow follow the movement of the camera.

2. Achieve crossbow charging

The animation controller transfer sequence is as follows, where hold and shoot are both hybrid trees.

Zero is an action in the zero-charge state, which is not provided in the resources. You can make a copy of the Fill animation, then open the editor and change all key frames to be the same as the initial frame, so that it is a still animation without charging. Add the bool condition "Fire" in the hold->shoot transfer process.

Add an animation event to the last frame of the shoot animation to trigger the script function Shoot to launch the arrow. Add an animation event to the empty animation and call the script function EnterEmpty to reset the Blend parameter to 0. Mount the following script to the crossbow. In this way, you can press the right button to charge up, and after charging, press the left button to launch.

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

public class FireControl : MonoBehaviour
{
    private Animator ani;
    public float fulltime;
    public float maxspeed;
    private float holdtime = 0;
    public GameObject prefab; // 预制对象的引用

    // Start is called before the first frame update
    void Start()
    {
        ani = GetComponent<Animator>();
    }

    private void FixedUpdate()
    {
        AnimatorStateInfo stateInfo = ani.GetCurrentAnimatorStateInfo(0);
        if (Input.GetButton("Fire1"))
        {
            ani.SetBool("Fire", true);
        }
        else
        {
            ani.SetBool("Fire", false);
        }
        if(Input.GetButton("Fire2"))
        {
            ani.SetBool("Holding",true);
            if(holdtime < fulltime && ani.GetCurrentAnimatorStateInfo(0).IsName("hold"))
                holdtime += Time.deltaTime;
            ani.SetFloat("Blend", holdtime / fulltime);
        }
        else
        {
            ani.SetBool("Holding", false);
        }
    }
    // Update is called once per frame
    void Update()
    {
    }
    public void EnterEmpty()
    {
        holdtime = 0;
        ani.SetFloat("Blend", 0);
    }
    public void Shoot(string name)
    {
        Debug.Log("Shoot");
        if (name == "shoot")
        {
            SSDirector.getInstance().currentSceneController.Shoot(ani.GetFloat("Blend") * maxspeed);//发射箭矢
        }
    }
}
3. Draw terrain, grass, and trees

The tree needs to manually add the component "Capsule Collider" to the prefab to add a collision body.

4. Make static targets and moving targets

Stationary targets are colored blue and moving targets are colored red

Add animation to the moving target. Open the animation editor via Window->Animation->Animation. Select the moving target game object->Select to add animation->Add the position attribute of the moving target->Set three key frames, the initial frame coordinates are (0,0,0), and the position coordinates of the last frame are the same as the initial frame . Then create an animation controller and set the following animation transfer:

You can select targetMove1 to modify its playback speed. Finally, add the animation controller to the target and check "Apply Root Motion" so that the prefab can be copied to different positions for round-trip motion.

5. Set up the shooting area

For gameplay reasons, the shooting area should be visible to the player. Here the shooting area is designed as a translucent blue square:

Check the Box Collider's "Is Trigger" option so the player can move through the box. Add a script to the block to implement OnTriggerEnter and OnTriggerExit to notify the event record object of the player's entry and exit of the shooting area respectively. The field recorder marks the boolean variable shootArea as true when the player enters the shooting area and false when leaving. At the same time, add an additional condition "ShootArea" to the crossbow's animation transfer "hold"->shoot, and synchronize it with the shootArea, so that the shoot animation cannot be triggered when the player is not in the shooting area, and the arrow will not be shot.

In addition to the shooting area, I also designed a supply area. Add bool condition HasArrow for animation transfer Empty->hold. When entering the supply area, change the number of remaining arrows to 10. Each time an arrow is shot, it will be reduced by 1. When it is reduced to 0, change HasArrow to false, and when entering the supply area, change it to true. This way the reloading animation will not play when there are no arrows.

6. Skybox

Use Fantasy Sky FREE sky material.

Cover half the game area with a giant cube trigger. A sky switch is triggered when the player enters or exits this collider. The script to switch the sky is as follows:

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

public class SkyControl : MonoBehaviour
{
    public Material[] mats;
    private static int index = 0;
    public int changeTime;//更换天空盒子的秒数

    public  void ChangeBox()
    {
        Debug.Log("change skybox"+index);
        RenderSettings.skybox = mats[index];
        index++;
        index %= mats.Length;
    }
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
    }
}

Drag the sky box material into the mats array on the unity editor page. Only two sky box materials are added here, so that the two areas and the sky box have a fixed mapping relationship.

7. Scoring

When shooting an arrow, name the arrow's game object "arrow". When the target collides, check whether the name of collision.gameObject is "arrow". If so, notify the field recorder and pass the current target's score (the public field of the script, It can be modified on the unity editor page). Each shooting area has its own score multiplier, and the multiplier is also passed to the scorer when entering the area. After receiving the hit event, the field recorder will multiply the target's score by the score multiplier of the current area as the score for this hit.

代码:3DgameDesign/lab11_Assets at main · xu-yongjia/3DgameDesign (github.com)

Video:unity homework-archery game_bilibili_bilibili

Guess you like

Origin blog.csdn.net/weixin_66458735/article/details/134667679