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

How to stop rotation

Discussion in 'Scripting' started by armorhide406, Jun 2, 2021.

  1. armorhide406

    armorhide406

    Joined:
    Mar 18, 2016
    Posts:
    16
    Trying to get a character to navigate, because they're essentially flying through a maze. I have raycasts set to short range to detect whether or not the character is close to the walls. I'm pretty sure I can get it to turn left or right depending on which wall it's closest too but I'm trying to figure out the easiest way to get it to stop turning.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Show the code you've got so far, we need to know your starting point to be able to help you.
     
  3. armorhide406

    armorhide406

    Joined:
    Mar 18, 2016
    Posts:
    16
    If it's all the same to you, I'd rather not. Like I said, I have raycasts working to be able to detect if it's close to walls and that should be able to set a variable like "ShouldTurnLeft" true. The trick is, conceptually what would be the best approach to stop rotation? Like make a Coroutine start when ShouldTurnLeft is true and then stop it when false or...?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    The thing is, "not turning" is sort of the default behavior of a object. So you're clearly doing something to make it turn. Without knowing what that something is, how are we supposed to know how to make it stop? Or if you haven't written the turning part yet, how valuable do you think our guidance will be if we tell you a method of turning that isn't compatible with whatever you already have going on?

    Well, I guess it's all the same to me, in that I will forget that this thread exists within a day. But presumably it's not all the same to you in that you need help, and we are happy to provide help, but we literally don't have the tools to help you if we can't see your current code.
    We're not going to steal it (it's not worth stealing). We're not going to make fun of you. We're not going to think less of you (the least we can possibly think of you is that you want us to just write the whole thing for you, and that's the opinion we start off with! It's only up from here.)
     
    bobisgod234 likes this.
  5. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    That sounds like you are asking how to make an object turn, not how to stop it from turning.

    StarManta is right. If you want your object to not turn, don't do anything. Not a whole lot more we can suggest if we don't know how it turns in the first place.

    Nobody will steal your code, especially considering it doesn't even work.
     
    Kurt-Dekker likes this.
  6. armorhide406

    armorhide406

    Joined:
    Mar 18, 2016
    Posts:
    16
    Well I figured it out, I just ran a coroutine and started it via boolean based on raycast hit detection. I just needed someone to say use a coroutine, because I'd tried to use it for something else but it didn't work out and I was afraid to use it. I'm not so concerned over stealing as much as formatting and naming convention

    Also it works, but it needs tweaking, because the map doesn't work out great for the side raycasts cause it ends up bumping into parts of the terrain. I suspect I'd have to add more raycasts or switch to trigger volumes to make it smoother

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BeastScript : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private int detectionRange;
    9.     [SerializeField]
    10.     private float attentionSpan;
    11.     [SerializeField]
    12.     private float wanderingSpeed;
    13.     [SerializeField]
    14.     private float huntSpeed;
    15.     [SerializeField]
    16.     private float minWanderDist;
    17.     [SerializeField]
    18.     private float maxWanderDist;
    19.     [SerializeField]
    20.     private Transform playerTarget;
    21.     [SerializeField]
    22.     private Rigidbody beastRB;
    23.     [SerializeField]
    24.     private float orbitAngle;
    25.     [SerializeField]
    26.     private bool heardPlayer;
    27.     [SerializeField]
    28.     private bool isHunting;
    29.     [SerializeField]
    30.     private bool isWandering;
    31.     //Navigation
    32.     [SerializeField]
    33.     private bool shouldTurnLeft;
    34.     [SerializeField]
    35.     private bool shouldTurnRight;
    36.     // Raycast sensors
    37.     [SerializeField]
    38.     private Transform floatBladderTransform;
    39.     RaycastHit depthHit;
    40.     [SerializeField]
    41.     private float depthRayDist;
    42.     [SerializeField]
    43.     private Transform whiskerLTransform;
    44.     [SerializeField]
    45.     private float leftDist;
    46.     RaycastHit leftHit;
    47.     [SerializeField]
    48.     private Transform whiskerRTransform;
    49.     [SerializeField]
    50.     private float rightDist;
    51.     RaycastHit rightHit;
    52.     //Trigger sensors
    53.     [SerializeField]
    54.     private BoxCollider leftProx;
    55.     [SerializeField]
    56.     private BoxCollider rightProx;
    57.  
    58.     int layerMask = 1 << 9;
    59.  
    60.     [SerializeField]
    61.     private CooldownSystem cooldownSystem;
    62.     [SerializeField]
    63.     private int thisID;
    64.  
    65.     public int ID => thisID;
    66.     public float cooldownDuration => attentionSpan;
    67.     // Start is called before the first frame update
    68.     void Start()
    69.     {
    70.         beastRB = this.GetComponent<Rigidbody>();
    71.         heardPlayer = false;
    72.         isWandering = true;
    73.         isHunting = false;
    74.         shouldTurnLeft = false;
    75.         shouldTurnRight = false;
    76.     }
    77.  
    78.     void FixedUpdate()
    79.     {
    80.         //movement
    81.         if (heardPlayer == false)
    82.         {
    83.             isWandering = true;
    84.         }
    85.         if (isWandering == true)
    86.         {
    87.             isHunting = false;
    88.             transform.position += transform.forward * wanderingSpeed * Time.deltaTime;
    89.         }
    90.         if (heardPlayer == true)
    91.         {
    92.             isHunting = true;
    93.         }
    94.         if (isHunting == true)
    95.         {
    96.             isWandering = false;
    97.             transform.LookAt(playerTarget);
    98.             transform.position += transform.forward * huntSpeed * Time.deltaTime;
    99.         }
    100.         //Turning bit
    101.         if (shouldTurnLeft == true)
    102.         {
    103.             StartCoroutine("TurnLeft");
    104.         }
    105.         else
    106.         {
    107.             StopCoroutine("TurnLeft");
    108.         }
    109.         if (shouldTurnRight == true)
    110.         {
    111.             StartCoroutine("TurnRight");
    112.         }
    113.         else
    114.         {
    115.             StopCoroutine("TurnRight");
    116.         }
    117.         //Hunting switch
    118.         if (cooldownSystem.isOnCooldown(thisID))
    119.         {
    120.             //If on "cooldown" then hunt. Kind of backwards but F*** it, I don't F***ing care, fite me, etc.
    121.             isHunting = true;
    122.             isWandering = false;
    123.             Debug.Log("is hunting" + isHunting);
    124.         }
    125.         if (!cooldownSystem.isOnCooldown(thisID))
    126.         {
    127.             isHunting = false;
    128.             isWandering = true;
    129.             //Debug.Log("is hunting" + isHunting);
    130.         }
    131.         //Raycast sensors
    132.         if (Physics.Raycast(floatBladderTransform.position, transform.TransformDirection(-Vector3.up), out depthHit, depthRayDist, layerMask))
    133.         {
    134.             Debug.DrawRay(floatBladderTransform.position, transform.TransformDirection(-Vector3.up) * depthHit.distance, Color.yellow);
    135.             //Debug.Log("Did Hit" + depthHit.distance.ToString());
    136.         }
    137.         if (Physics.Raycast(whiskerLTransform.position, transform.TransformDirection(-Vector3.right), out leftHit, leftDist, layerMask))
    138.         {
    139.             Debug.DrawRay(whiskerLTransform.position, transform.TransformDirection(-Vector3.right) * leftHit.distance, Color.yellow);
    140.             //Debug.Log("Did Hit" + leftHit.distance.ToString());
    141.             //adjust left/rightDist to adjust how close it can get
    142.             shouldTurnRight = true;
    143.         }
    144.         else
    145.         {
    146.             shouldTurnRight = false;
    147.         }
    148.         if (Physics.Raycast(whiskerRTransform.position, transform.TransformDirection(Vector3.right), out rightHit, rightDist, layerMask))
    149.         {
    150.             Debug.DrawRay(whiskerRTransform.position, transform.TransformDirection(Vector3.right) * rightHit.distance, Color.yellow);
    151.             //Debug.Log("Did Hit" + rightHit.distance.ToString());
    152.             shouldTurnLeft = true;
    153.         }
    154.         else
    155.         {
    156.             shouldTurnLeft = false;
    157.         }
    158.  
    159.         //Trigger sensors
    160.     }
    161.     void OnTriggerEnter(Collider other)
    162.     {
    163.         //Do thing
    164.         if (other.tag == "sonar")
    165.         {
    166.             Debug.Log("Heard Player's Sonar");
    167.             heardPlayer = true;
    168.             attentionSpan = 10f;
    169.             //Debug.Log(attentionSpan);
    170.             //cooldownSystem.putOnCooldown(this);
    171.         }
    172.         if (other.tag == "cavitate")
    173.         {
    174.             Debug.Log("Heard Player cavitate");
    175.             heardPlayer = true;
    176.             attentionSpan = 6f;
    177.             Debug.Log(attentionSpan);
    178.             //cooldownSystem.putOnCooldown(this);
    179.         }
    180.         if (other.tag == "crash")
    181.         {
    182.             Debug.Log("Heard Player crash");
    183.             heardPlayer = true;
    184.             attentionSpan = 8f;
    185.             Debug.Log(attentionSpan);
    186.             //cooldownSystem.putOnCooldown(this);
    187.         }
    188.     }
    189.     void OnTriggerExit(Collider other)
    190.     {
    191.         if (other.tag == "sonar")
    192.         {
    193.             heardPlayer = false;
    194.         }
    195.     }
    196.     IEnumerator TurnRight()
    197.     {
    198.         beastRB.transform.Rotate(Vector3.up);
    199.         yield break;
    200.     }
    201.     IEnumerator TurnLeft()
    202.     {
    203.         beastRB.transform.Rotate(-Vector3.up);
    204.         yield break;
    205.     }
    206. }
    207.  
     
    Last edited: Jun 3, 2021
  7. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Why exactly are these coroutines? All they do is execute one line of code and then stop, like an ordinary function. Why not just make them an ordinary function?

    For example:

    Code (CSharp):
    1.     void TurnRight()
    2.     {
    3.         beastRB.transform.Rotate(Vector3.up);
    4.     }
    Code (CSharp):
    1.         if (shouldTurnLeft == true)
    2.         {
    3.             TurnRight();
    4.         }
     
    PraetorBlue likes this.
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    I'm not sure what you think coroutines do but I'm pretty sure you have some misconceptions about them, because those are some extremely pointless coroutines. The entire purpose of coroutines in Unity is to run code over multiple frames, and your coroutines don't do that. Further, StopCoroutine on those coroutines is pointless as well because by the time StopCoroutine is being called on them, they've done everything they're ever going to do. (They do everything up to the first "yield" statement immediately when you call StartCoroutine)

    That part of the code can (probably should) be:
    Code (csharp):
    1. if (shouldTurnLeft) {
    2. beastRB.transform.Rotate(-Vector3.up);
    3. }
    4. if (shouldTurnRight) {
    5. beastRB.transform.Rotate(Vector3.up);
    6. }
    And even if coroutines were a part of the solution, I still don't know how you would have expected us to say "oh just use coroutines" with zero context.
     
  9. armorhide406

    armorhide406

    Joined:
    Mar 18, 2016
    Posts:
    16
    Yeah definitely misconceptions, although I swear I provided enough context short of the code.

    But having the booleans in place activated by raycasts isn't too roundabout right? Or should it just be executing those transform.rotate on hit?