Jump to content

Weapon Sway Effect in C# - Unigine 2.13.0.0 Community


photo

Recommended Posts

Hello to all ambitious creators!
I have been using Unigine for a short time and initially I have to say that it is very pleasant to work with. Unfortunately, I cannot consider myself quite familiar with this engine, so I assume this post, maybe it will help someone in the future.

I would like to ask for help in creating a script that would be responsible for the "Sway Effect" of weapon when moving the mouse. I mean the weapon sway smoothly from side to side, and smoothly return to it's default position. It would be nice if someone helped me with it and explained in some way how it works, so that in the future I could understand more easily how it's done. I would like to get the effect of not only changing the position of the weapon from the left to the right and vice versa, but also the effect of rotating around axis (something like that: YouTube Link).

I will add that I am interested in the script in the C#. I tried my hand at it (previously I worked in the Unity Engine), but I don't know how to do it in Unigine, unfortunately.

 

ps. I don't use English on a daily basis, I apologize in advance if I have made any linguistic mistakes :P

 

Edited by VegetableGames
EDIT: mistake in YouTube link, replaced :P
Link to comment

Little Update!

I made this script and it managed to do something, but I have a problem with smooth. Anyone know how this can be fixed?

Script and video attached below.

Video:

2020-12-03 14-26-37.mkv

Script:

using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;

[Component(PropertyGuid = "7cbb2e62adfd5a59d6fcffe4f280c9f3962b06b6")]
public class SwayEffect : Component
{
    public float amount = 0.02f;
    public float maxAmount = 0.03f;
    public float smooth = 3.0f;

    private vec3 _def;

    void Init() {
        _def = node.Position;
    }

    public void Update() {
        float _factorX = -Input.MouseDelta.x * amount;
        float _factorY = -Input.MouseDelta.y * amount;
        float _factorZ = -Input.MouseDelta.x * amount;

        if (_factorX > maxAmount) {
            _factorX = maxAmount;
        }

        if(_factorX < -maxAmount) {
            _factorX = -maxAmount;
        }

        if (_factorY > maxAmount) {
            _factorY = maxAmount;
        }

        if (_factorY < -maxAmount) {
            _factorY = -maxAmount;
        }

        if (_factorZ > maxAmount) {
            _factorZ = maxAmount;
        }

        if (_factorZ < -maxAmount) {
            _factorZ = -maxAmount;
        }

        node.Position = vec3.Lerp(_def, new vec3(_def.x + _factorX, (_def.y + _factorY) / 1.25f, (_def.z + _factorZ) / 3.65f), (amount * smooth) * Game.IFps);
    }
}

 

2020-12-03 14-26-37.mkv

Link to comment

Okay, I fixed that!

I've changed:

float _factorX = -Input.MouseDelta.x * amount;
float _factorY = -Input.MouseDelta.y * amount;
float _factorZ = -Input.MouseDelta.x * amount;

to:

//In Class
private float _factorX;
private float _factorY;
private float _factorZ;

//In void Update
_factorX = MathLib.Lerp(_factorX, -Input.MouseDelta.x * amount, Game.IFps * 10f);
_factorY = MathLib.Lerp(_factorY, -Input.MouseDelta.y * amount, Game.IFps * 10f);
_factorZ = MathLib.Lerp(_factorZ, -Input.MouseDelta.x * amount, Game.IFps * 10f);

 

Also problem solved! I am leaving this post here, maybe it will help someone :P

  • Like 2
Link to comment

@silent Yup, here you are :) - YouTube Link
I'm just getting started with Unigine, but I'm going to switch from Unity to this Engine because it surprised me positively!

I need to add a few more details, but hey - it finally works!

It's amazing how this engine can be understood quickly, I also made this scene (picture attached below) after just 3 hours of fun with Unigine :P

Unigine Scene.png

  • Like 4
Link to comment
×
×
  • Create New...