[Unity tips] Realize the effect of FPS shooting game gun weapons swinging with the camera arm

Preface

If our perspective moves and turns, if the weapon does not swing accordingly, our movements will feel stiff, especially in shooting games, as follows
Insert image description here

To implement weapon swing, we mainly share two implementation methods, one is to change the position of the weapon, and the other is to modify the rotation of the weapon.

Method 1. Change the position of the weapon

public class WeaponSway : MonoBehaviour
{
    
    
    /* 摇摆的参数 */
    public float amount; // 摇摆幅度
    public float smoothAmount; // 平滑值
    public float maxAmount; // 最大摇摆幅度
    private Vector3 originalPosition; // 初始位置

    void Start()
    {
    
    
        // 自身位置(相对于父级物体变换得位置)
        originalPosition = transform.localPosition;
    }

    void Update()
    {
    
    
        // 设置武器手臂模型位置的值,(鼠标反转)
        float movementX = -Input.GetAxis("Mouse X") * amount;
        float movementY = -Input.GetAxis("Mouse Y") * amount;

        // 限制摇摆范围
        movementX = Mathf.Clamp(movementX, -maxAmount, maxAmount);
        movementY = Mathf.Clamp(movementY, -maxAmount, maxAmount);
        Vector3 finalPosition = new Vector3(movementX, movementY, 0);

        // 手柄位置变换
        transform.localPosition = Vector3.Lerp(transform.localPosition, finalPosition + originalPosition, Time.deltaTime * smoothAmount);
    }
}

Mount configuration parameters
Insert image description here
Effect
Insert image description here

Method 2. Change weapon rotation

public class WeaponSway2 : MonoBehaviour
{
    
    
    public float amount; // 摇摆幅度
    public float smooth; // 平滑度 

    private void Update()
    {
    
    
        // 获取鼠标输入
        float mouseX = Input.GetAxisRaw("Mouse X") * swayMultiplier;
        float mouseY = Input.GetAxisRaw("Mouse Y") * swayMultiplier;
        

        // 计算目标旋转
        Quaternion rotationX = Quaternion.AngleAxis(-mouseY, Vector3.right);
        Quaternion rotationY = Quaternion.AngleAxis(mouseX, Vector3.up);
        Quaternion targetRotation = rotationX * rotationY;

        // 旋转
        transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
    }
}

Mount configuration parameters
Insert image description here
Effect
Insert image description here

Conclusion

You can use any method, it depends on what you like. If it were me, I would choose the second method, which is simpler, has fewer parameters, and personally feels the effect is better. Rotating the center of the gun is more realistic.

end

Giving roses to others will leave a lingering fragrance in your hands! If the content of the article is helpful to you, please don't be stingy with your 点赞评论和关注 so that I can receive feedback as soon as possible. Every time you 支持 The greatest motivation for creation. Of course, if you find 存在错误 or 更好的解决方法 in the article, you are welcome to comment and send me a private message!

Good, I am向宇,https://xiangyu.blog.csdn.net

A developer who has been working quietly in a small company recently started studying Unity by himself out of interest. If you encounter any problems, you are also welcome to comment and send me a private message. Although I may not necessarily know some of the questions, I will check the information from all parties and try to give the best suggestions. I hope to help more people who want to learn programming. People, encourage each other~
Insert image description here

Guess you like

Origin blog.csdn.net/qq_36303853/article/details/134884078