Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Trying to move objects along Y axis while holding them

Discussion in 'Scripting' started by klousewolf, Sep 22, 2021.

  1. klousewolf

    klousewolf

    Joined:
    Sep 22, 2021
    Posts:
    1
    I am trying to create a game that uses mechanics like in "Superliminal", I have code that is somewhat working, it will allow me to grab an object and based on how far away the wall I look at is the object will be scaled so the perspective looks the same. My issue is that I am locked to the X axis when moving the objects. I want to be able to freely move the object around like in Superliminal. Example of what I want it to do vs What it does (also unable to grab objects that become too small, gonna worry about fixing that later)

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class scaleray : MonoBehaviour
    4. {
    5.    
    6.     [Header("Components")]
    7.     public Transform target;            // The target object we picked up for scaling
    8.  
    9.     [Header("Parameters")]
    10.     public LayerMask targetMask;        // The layer mask used to hit only potential targets with a raycast
    11.     public LayerMask ignoreTargetMask;  // The layer mask used to ignore the player and target objects while raycasting
    12.     public float offsetFactor;          // The offset amount for positioning the object so it doesn't clip into walls
    13.  
    14.     float originalDistance;             // The original distance between the player camera and the target
    15.     float originalScale;                // The original scale of the target objects prior to being resized
    16.     Vector3 targetScale;                // The scale we want our object to be set to each frame
    17.  
    18.     [SerializeField] private string TargetableTag = "Targetable";
    19.     [SerializeField] private Material highlightMaterial;
    20.     [SerializeField] private Material defaultMaterial;
    21.  
    22.     private Transform _selection;
    23.    
    24.    
    25.     void Start()
    26.     {
    27.         Cursor.visible = false;
    28.         Cursor.lockState = CursorLockMode.Locked;
    29.     }
    30.  
    31.     void Update()
    32.     {
    33.         Highlight();
    34.         HandleInput();
    35.         ResizeTarget();
    36.     }
    37.  
    38.    private void Highlight()
    39.     {
    40.     if (_selection != null)
    41.         {
    42.             var selectionRenderer = _selection.GetComponent<Renderer>();
    43.             selectionRenderer.material = defaultMaterial;
    44.             _selection = null;
    45.         }
    46.        
    47.         var ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2f, Screen.height/2f, 0f));
    48.         RaycastHit hit;
    49.         if (Physics.Raycast(ray, out hit))
    50.         {
    51.             var selection = hit.transform;
    52.             if (selection.CompareTag(TargetableTag))
    53.             {
    54.                 var selectionRenderer = selection.GetComponent<Renderer>();
    55.                 if (selectionRenderer != null)
    56.                 {
    57.                     selectionRenderer.material = highlightMaterial;
    58.                 }
    59.  
    60.                 _selection = selection;
    61.             }
    62.         }
    63.     }
    64.    
    65.     private void HandleInput()
    66.     {
    67.                     // Check for left mouse click
    68.                     if (Input.GetMouseButtonDown(0))
    69.                     {
    70.                             // If we do not currently have a target
    71.                             if (target == null)
    72.                             {
    73.                                 // Fire a raycast with the layer mask that only hits potential targets
    74.                                 RaycastHit hit2;
    75.                                     if (Physics.Raycast(transform.position, transform.forward, out hit2, Mathf.Infinity, targetMask))
    76.                                     {
    77.                                         // Set our target variable to be the Transform object we hit with our raycast
    78.                                         target = hit2.transform;
    79.  
    80.                                         // Disable physics for the object
    81.                                         target.GetComponent<Rigidbody>().isKinematic = true;
    82.                                         //test to remove collision
    83.                                         target.GetComponent<Rigidbody>().detectCollisions = false;
    84.  
    85.                                         // Calculate the distance between the camera and the object
    86.                                         originalDistance = Vector3.Distance(transform.position, target.position);
    87.  
    88.                                         // Save the original scale of the object into our originalScale Vector3 variabble
    89.                                         originalScale = target.localScale.x;
    90.  
    91.                                         // Set our target scale to be the same as the original for the time being
    92.                                         targetScale = target.localScale;
    93.                                     }
    94.                             }
    95.                         // If we DO have a target
    96.                         else
    97.                         {
    98.                             // Reactivate physics for the target object
    99.                             target.GetComponent<Rigidbody>().isKinematic = false;
    100.                                 //test to restore collision
    101.                                 target.GetComponent<Rigidbody>().detectCollisions = true;
    102.  
    103.                             // Set our target variable to null
    104.                             target = null;
    105.                     }
    106.             }
    107.         }
    108.  
    109.    
    110.    
    111.     void ResizeTarget()
    112.     {
    113.         // If our target is null
    114.         if (target == null)
    115.         {
    116.             // Return from this method, nothing to do here
    117.             return;
    118.         }
    119.  
    120.         // Cast a ray forward from the camera position, ignore the layer that is used to acquire targets
    121.         // so we don't hit the attached target with our ray
    122.         RaycastHit hit;
    123.         if (Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity, ignoreTargetMask))
    124.         {
    125.             // Set the new position of the target by getting the hit point and moving it back a bit
    126.             // depending on the scale and offset factor
    127.             target.position = hit.point - transform.forward * offsetFactor * targetScale.x;
    128.  
    129.             // Calculate the current distance between the camera and the target object
    130.             float currentDistance = Vector3.Distance(transform.position, target.position);
    131.  
    132.             // Calculate the ratio between the current distance and the original distance
    133.             float s = currentDistance / originalDistance;
    134.  
    135.             // Set the scale Vector3 variable to be the ratio of the distances
    136.             targetScale.x = targetScale.y = targetScale.z = s;
    137.  
    138.             // Set the scale for the target objectm, multiplied by the original scale
    139.             target.localScale = targetScale * originalScale;
    140.         }
    141.     }
    142. }