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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Limit Y axis movement, allow -y axis.

Discussion in 'Scripting' started by Greywolf6, Oct 23, 2021.

  1. Greywolf6

    Greywolf6

    Joined:
    Oct 20, 2020
    Posts:
    10
    I placed notes along the way to help explain. Basically I need the Y axis locked until the ground tile catches you. Then you need to fall. Right now you just fly around and the game does not end.


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class FreezePosition : MonoBehaviour
    7. {
    8.     Rigidbody sphereRB;
    9.     private bool isCarGrounded;
    10.     public LayerMask groundLayer;
    11.  
    12.    
    13.    
    14.  
    15.     // Update is called once per frame
    16.     void FixedUpdate()
    17.     {
    18.             //This keeps the vehicle working and gives the if statement information.
    19.         RaycastHit hit;
    20.         isCarGrounded = Physics.Raycast(transform.position, -transform.up, out hit, 1f, groundLayer);
    21.  
    22.  
    23.             //This stops a bobble in Y axis from the vehicle controller and I get a debug log in the console.
    24.  
    25.         sphereRB = GetComponent<Rigidbody>();
    26.         sphereRB.constraints = RigidbodyConstraints.FreezePositionY |   RigidbodyConstraints.FreezePositionY;
    27.         Debug.Log("locked");
    28.     }
    29.             //This is supposed to turn off the constraints so the vehicle will fall when you run off ground.
    30.             //I get a debug print in console that it ran, but the vehicle does not fall.
    31.     void Update()
    32.     {
    33.         if (isCarGrounded)
    34.         {
    35.             return;
    36.         }
    37.         else
    38.         {
    39.             sphereRB.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
    40.         }
    41.        
    42.  
    43.     }
    44.  
    45.  
    46. }
    47.  
    48. // I have no idea on how to get this to work.  Ethier the vehicle wont fall, or the bobbel is back.  I have tried them under start
    49. // and every place else along with many other things.
    50.  
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    So if I'm understanding correctly, you want to constrain the Y position while the car is 1 unit from the ground?

    There's a few errors I noticed in your code, which didn't really help trying to understand what you were doing. Here's what I presume you wanted, but cleaned up.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class FreezePosition : MonoBehaviour
    5. {
    6.     Rigidbody sphereRB;
    7.     public LayerMask groundLayer;
    8.  
    9.     void Start()
    10.     {
    11.         sphereRB = GetComponent<Rigidbody>(); // don't need to set the reference every time
    12.     }
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         RaycastHit hit;
    17.         // why did you use -transform.up when there's a transform.down?
    18.         bool isCarGrounded = Physics.Raycast(transform.position, transform.down, out hit, 1f, groundLayer);
    19.  
    20.         if (isCarGrounded)  // no reason to wait until Update to change this, why not change it at the time of raycast
    21.         {
    22.             // here you did FreezePositionY | FreezePositionY, where I think you wanted one of them to be rotation
    23.             sphereRB.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationY;
    24.             Debug.Log("locked");
    25.         }
    26.         else
    27.         {
    28.             sphereRB.constraints = RigidBodyConstraints.None;
    29.             Debug.Log("unlocked");
    30.         }
    31.     }
    32. }
     
  3. Greywolf6

    Greywolf6

    Joined:
    Oct 20, 2020
    Posts:
    10
    I appreciate your work, it, however did not do what I needed.

    What I need to do is have the sled run dead flat no upward movement (keeps player from going up the canyon wall). I achieved the dead flat driving by using the constraints in the rigid body inspector freezing the Y rotation and it worked like a charm until, the collapsing ground tiles catch up with you. Then you are locked in the Y axis, don't fall and the game basically breaks because there is no end.

    What I am trying to do with this script is lock the vehicle in Y axis until there is no ground and then it can fall. Once it falls, there is a trigger that will end the game.

    The script for the vehicle is a hodgepodge of other scripts and the vehicle works fine except for this one part.