Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Need help making switch shoulders script

Discussion in 'Scripting' started by ScriptyIRL, Jun 30, 2023.

  1. ScriptyIRL

    ScriptyIRL

    Joined:
    May 5, 2022
    Posts:
    2
    Hi everyone. This is my first time posting here and I've been needing help quite some time now. I'm making a game and I want to add a feature that when I press V, it switches shoulders. I'm kinda new to Unity so don't mind teaching me new stuff.

    I wanted to make a script that allows me to switch camera side from the CineMachineVirtualCamera component:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class CameraSideChanger : MonoBehaviour
    5. {
    6.     public Cinemachine.CinemachineVirtualCamera virtualCamera;
    7.     private float currentXPosition;
    8.  
    9.     private void Start()
    10.     {
    11.         currentXPosition = 0f;
    12.     }
    13.  
    14.     private void Update()
    15.     {
    16.         if (Keyboard.current.vKey.wasPressedThisFrame)
    17.         {
    18.             currentXPosition = (currentXPosition == -0.6f) ? 0.6f : -0.6f;
    19.             UpdateCameraXPosition();
    20.         }
    21.     }
    22.  
    23.     private void UpdateCameraXPosition()
    24.     {
    25.         Cinemachine.CinemachineTransposer transposer = virtualCamera.GetCinemachineComponent<Cinemachine.CinemachineTransposer>();
    26.         if (transposer != null)
    27.         {
    28.             Vector3 newPosition = transposer.m_FollowOffset;
    29.             newPosition.x = currentXPosition;
    30.             transposer.m_FollowOffset = newPosition;
    31.         }
    32.     }
    33. }
    34.  


    It's meant to change this value:
    upload_2023-6-30_20-44-22.png

    I've attached it into an empty gameobject and that's it.


    Please help!!!
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I think you want to have two virtual cams, one for each shoulder, then tell Cinemachine to switch smoothly between them.

    There's also a dedicated cinemachine forum: https://forum.unity.com/forums/cinemachine.136/

    OR if you want to do the tweening yourself, then use Vector3.Lerp and your "camera side" variable to generate a position between the two shoulder positions.

    If you need it to arc up and over the head, you would need some kind of path specification mechanism such as a bezier curve going from shoulder to shoulder, and use that to tween.

    Smoothing any scalar value between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4

    Another approach would be to use a tweening package such as LeanTween, DOTween or iTween.

    OR... just make a simple canned animation... No code needed!

    For score count-up (like a cash register whizzing lots of numbers upwards), I like this approach:

    https://forum.unity.com/threads/create-score-increasing-effect.1273673/#post-8082548
     
    Yoreki likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    Here's a video tutorial for it with two different approaches to switching (state-based and code-based). Code is the easiest if you're not expecting to have many cameras as you're just adjusting the priority of the cameras.

     
    Yoreki likes this.
  4. ScriptyIRL

    ScriptyIRL

    Joined:
    May 5, 2022
    Posts:
    2
    Thanks guys. Both of you.
     
    Kurt-Dekker likes this.