torsdag 25 november 2021

Smooth Zooming with Cinemachine (Unity)

Zooming a Cinemachine Virtual Camera with 3rd Person Follow by changing the camera distance works but there are no transitions between the steps.

There are other ways to zoom that look better.

One way to get it smoother I found out  is to add a extension to your virtual camera called Follow Zoom. It's a script and comes with values you can change in the inspector. The Follow Zoom extension can be added at the bottom of your virtual camera, in the inspector.

You can use the input you use for zoom to alter these values in a script of your own. You use a reference to your virtual camera and do .GetComponent<CinemachineFollowZoom>() to get the Follow Zoom script.

Once you have the Follow Zoom extension you can change the values of these variables to get a desired transition in your zooming:

_cinemachineFollowZoom.m_MinFOV += zoomInput;

_cinemachineFollowZoom.m_MaxFOV += zoomInput;

_cinemachineFollowZoom.m_Width += zoomInput;


ZoomInput above is calculated from the Input Action Asset made in "new" Input System. It's a Vector2, triggered by scrolling and pressing the gamecontroller defined in a created input asset, attached to a Player Input component, added to the player GameObject. Google Unity Input System to learn how to use it. It's not as complicated as it might look.


You will need checks to make sure you stay within the limits set in the inspector.

These conditions aren't allowed:

if (zoomInput > 0 && cineMinFoV >= cineMaxFoV)

if(zoomInput < 0 && cineMinFoV < 1)


I found a value of about 1-4 is pretty good to set "Damping" to in the inspector and Min FOV to 2. Damping needs to be above 0.

måndag 15 november 2021

Unity - How to get access and change the camera distance while using Cinemachine "3rd person follow" (C#)

Cinemachine https://unity.com/unity/features/editor/art-and-design/cinemachine

saves time by letting you create difference kinds of cameras.


Let's say you want to use the input system and scroll the mouse to change the camera distance.

I couldn't find a clear solution online so I thought I share:


First make sure you have:

A player GameObject.

A GameObject with a CinemachineVirtualCamera, set to: 

* follow the player.

* its "Body" set to "3rd Person Follow".


Note: You have other body properties as well to chose from:

3rd Person follow

Framing Transposer

Hard Lock to Target

Orbital Transposer

Tracked Dolly

Transposer


We are focusing on the "3rd Person Follow" now.


In the script that you add as component to the player GameObject:


Declaration: 

private GameObject _playerFollowCamera;

public CinemachineVirtualCamera _cinemachineVirtualCamera;


In private void Start() :

_playerFollowCamera = GameObject.Find("PlayerFollowCamera");

_cinemachineVirtualCamera = _playerFollowCamera.GetComponent<CinemachineVirtualCamera>();


          In your method, called when scrolling for example: 

CinemachineComponentBase cinemachineComponentBase = _cinemachineVirtualCamera.GetCinemachineComponent(CinemachineCore.Stage.Body);


if (cinemachineComponentBase is Cinemachine3rdPersonFollow)

{

(cinemachineComponentBase as Cinemachine3rdPersonFollow).CameraDistance = yourCameraDistanceValue;

}


Or as one line:

(GameObject.Find("PlayerFollowCamera").GetComponent<CinemachineVirtualCamera>().GetCinemachineComponent(CinemachineCore.Stage.Body) 

as Cinemachine3rdPersonFollow).CameraDistance = yourCameraDistanceValue;


"Cinemachine3rdPersonFollow" above is changed depending on which body property you are using.


måndag 4 januari 2016

GTA V: Change vehicle sounds on any vehicle to any sound

It's really easy to change the sounds on any vehicle to any other vehicle sound in GTA V.

Before we start, make sure to use OpenIV with the mods folder for safety: http://openiv.com/?p=1132

This is how you do it:
1. Go to update\update.rpf\common\data\levels\gta5 with OpenIV
2. Right click on vehicles.meta and press "Save content/ Export".
3. Open the file that you exported with notepad.
4. Find the vehicle model you want to change (CTRL+F and then search for modelName). If you are unsure which model it is you can use Native Trainer to spawn them and find out.
5. Look for "<audioNameHash />" under your model name. Replace it with "<audioNameHash>PutYourModelNameHere</audioNameHash>" and instead of "PutYourModelNameHere" you write the name of the model you want to use the sound from. Here are all model names: http://pastebin.com/i4AX8kBY (taken from Alexander Blades Native Trainer source).
6. Save the file, go back to OpenIV and replace the vehicles.meta with your version of it.
7. Start GTA V try it out :)

Tip: A sound that I like a lot is the engine sound of Stirling GT with model name FELTZER3. That would be <audioNameHash>FELTZER3</audioNameHash>

Play GTA V without violence

Family Friendly Free Roaming has been out for a while now. FFFR is a modification for Grand Theft Auto 5 that disables violence in the game. The latest versions has made it even less violent. This means that even children can play it.

Except making the game less violent, the purpose of the mod is to make it more fun exploring the amazing world without interrupting elements.

There are also features like entering vehicles as passenger and busing pedestrians around.

You can read more and get the latest version of FFFR here: https://www.gta5-mods.com/scripts/family-friendly-free-roaming


onsdag 22 juli 2015

GTA V Scripting PC Part 3: Keyboard and Controller support

Keyboard support example

For keyboard support you need these functions:

bool get_key_pressed(int nVirtKey)
{
    return (GetAsyncKeyState(nVirtKey) & 0x8000) != 0;
}

We need to check if the key is valid with isprint(): http://www.cplusplus.com/reference/cctype/isprint/
We also want a delay between possible keystrokes. We use GetTickCount() for this:

DWORD trainerResetTim;

void reset_mod_switch()
{
    trainerResetTime = GetTickCount();
}

We want to get the key pressed every 400 milliseconds. Insert is used as key if activateKeyChar is not a valid key:

bool on_off_switch_pressed(){
    if (isprint(activateKeyChar)){ // activateKeyChar is your key
        return ((GetTickCount() > trainerResetTime + 400) &&   get_key_pressed(activateKeyChar));
    }
    else{
        return ((GetTickCount() > trainerResetTime + 400) && get_key_pressed(0x2D));
    }
}

Read the previous post about how to read from the .ini file. You can in this way let the user choose a key code from here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx and write it in the .ini file so that your mod can get it and use it.

Now you can in your script loop check if your button is pressed:

if (on_off_switch_pressed()){
// do something
}

Controller support example

Controller support is similar:

bool on_off_mod_with_controller_switch_pressed(){
    return ((GetTickCount() > trainerResetTime + 400) && isControllButtonPressed(activateControllerButtonChar)
        && canActivateWithController);
}

canActivateWithController is a bool used to make it so that the controller only is usable when wanted.

Controller button keys:http://pastebin.com/X6X2hUB7

int xInputIndex = 2; // always 2
bool isControllButtonPressed(int controllerButton){
    if (canActivateWithController){
        if (CONTROLS::IS_CONTROL_PRESSED(xInputIndex, controllerButton)){
            return true;
        }
    }
    return false;
}



fredag 10 juli 2015

GTA V Scripting PC Part 2: How to read and write from configuration files (.ini).

Often you want your script to be customizable. This can be done by reading and writing to a configuration file that the script uses.

Files can be set to be read from continuously, which lets you make changes while playing and those changes to be updated directly without having to restart the game.

First, lets create a new file in your GTA5 folder where your GTA5.exe is located. Name it "VehicleColor.ini". Make sure the file ends with .ini and not .txt.
 Open the file and add this:

[Vehicle_Color]
red=50
green=50
blue=200

Save the file and start Visual Studio. For this new project you need a new copy of the Script Hook SDK. You can go back and watch the installation tutorial that Johnny Manson made if you like, linked to in my last post.

Inside the SDK, open up the project called "NativeTrainer.sln" inside the samples folder. Now, replace everything inside the "script.cpp" file with the code here.

With this simple mod we want to change the color of the vehicle we are currently using. So we need to get a reference to the players car. Replace this line "PED::SET_PED_CAN_BE_DRAGGED_OUT(playerPed, false);"
with
if (PED::IS_PED_IN_ANY_VEHICLE(PLAYER::PLAYER_PED_ID(), true)){
Vehicle playerVeh;
}

Now we need the RGB values from our .ini file so that we can paint our vehicle.

Add these values and function to the top of the script.

int red, green, blue;

void updateConfigValues(){
    red = GetPrivateProfileInt("Vehicle_Color", "red", -1, ".\\VehicleColor.ini");
    green = GetPrivateProfileInt("Vehicle_Color", "green", -1, ".\\VehicleColor.ini");
    blue = GetPrivateProfileInt("Vehicle_Color", "blue", -1, ".\\VehicleColor.ini");
}

Now it's time to do something with our RGB value.

VEHICLE::SET_VEHICLE_CUSTOM_PRIMARY_COLOUR(playerVeh, red, green, blue);
VEHICLE::SET_VEHICLE_CUSTOM_SECONDARY_COLOUR(playerVeh, red, green, blue);
VEHICLE::SET_VEHICLE_TYRE_SMOKE_COLOR(playerVeh, red, green, blue);    VEHICLE::_SET_VEHICLE_NEON_LIGHTS_COLOUR(playerVeh, red, green, blue);

There's no need to run this too often so we tell the script to wait for 2 seconds. Under update_features(); in the while loop add "WAIT(2000);"

Now it should look something like this

Now when you're done, build the solution in Visual Studio and place the .asi file into your GTA5 folder where your GTA5.exe is located. TAB down during gameplay, open up your VehicleColor.ini file and change the three numbers/colors to something else (0-255). Save the file and TAB back into the game. Your vehicles color should now have been changed.

Now it's time to test it and add more code to it. :)

Writing to a file is pretty straight forward, it looks something like this:

WritePrivateProfileString(TEXT("Vehicle_Color"),
    TEXT("red"),
    TEXT("255"),
    TEXT(".\\VehicleColor.ini");
    }

Next post about support for keyboard and controller http://mwasteson.blogspot.se/2015/07/gta-v-scripting-pc-part-3-keyboard-and.html

How to make scripts for GTA V PC

Scripting mods for GTA V is actually quite easy. I will show you how to create some neat stuff. First, let's watch this GTA V scripting tutorial made by Johnny Manson, about how to get started and making your first script.


Johnny is directly using C++ to code with. You can also add scripts that let you write in .NET and LUA. In this tutorial we will only write in C++

So, Jonny gave us a template to start with. I made it even smaller and you can check it out here: code.
Mods are of course usually larger. You can remove the line "PED::SET_PED_CAN_BE_DRAGGED_OUT(playerPed, false);" and replace with something else.
Download the latest Script Hook SDK and replace everything in the file "script.cpp" with the contents of code.

Before you TAB down from the game to make changes in your code in visual studio, hold CTRL and press R. A small beep will sound which means that all mods are inactivated. You can now create a new version of your mod and replace the old one with it. When you're done press CTRL+R again to reload all your mods. You should hear three beeps. Note that for this to work you need an empty file named "ScripthookV.dev" in your game folder.

When you start scripting you need some sources and information about how to do different things. The above all most important source is Alexander Blades http://www.dev-c.com with the native database /nativedb/. Not everything is known about how functions work or which parameters are used. Sometimes you need to guess or try and error.

When your mod is ready for it's first release you can publish it on sites like gta5-mods.com and gtainside.com.

In the next post we will look into how to read and write to configuration files (.ini).