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 Why won't this code work?

Discussion in 'Scripting' started by Moogamouth828, Nov 7, 2020.

  1. Moogamouth828

    Moogamouth828

    Joined:
    Feb 8, 2020
    Posts:
    12
    I wish I could give some more description, but there are a ton of weird things that happen when I run this very simple code.
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. public class Seizure : MonoBehaviour
    6. {
    7.  
    8.     void Update()
    9.     {
    10.         transform.Rotate(new Vector3(Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360)));
    11.         transform.Translate(new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100)));
    12.         transform.Rotate(new Vector3(-transform.rotation.x, -transform.rotation.y, -transform.rotation.z));
    13.         transform.Translate(new Vector3(-transform.position.x, -transform.position.y, -transform.position.z));
    14.  
    15.     }
    16.  
    17. }
    18.  
    19.  
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,579
    Describe what you mean by "weird things".
    What do you expect to happen and what is happening.
     
  3. Moogamouth828

    Moogamouth828

    Joined:
    Feb 8, 2020
    Posts:
    12
    Here's a video of what happens. I simply want it to rapidly change the camera position and rotation to a random amount. Please realize that this is a very, very beginner project. It's just a starter project.
     

    Attached Files:

  4. Moogamouth828

    Moogamouth828

    Joined:
    Feb 8, 2020
    Posts:
    12
    And it kind of does it, but then it breaks down. Also, it seems to be changing the coordinates to amounts that are past the random ranges.
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,579
    You run your script in Update, which means, will change every frame.
    Is that your goal? If so, it looks it works as expected.
    However, it is too chaotic, to deduct correct behaviour.

    What you try to do in line 12 and 13?

    You need make step by step changes. Add some bool in If condition, as public. i.e.
    Code (CSharp):
    1. public bool canChangeRotation
    That will add it to inspector.
    Then inside update
    Code (CSharp):
    1. if ( !canChangeRotation ) return ;
    2. canChangeRotation = false ;
    Trigger bool from inspector, and observe what happens with translation.
     
  6. Moogamouth828

    Moogamouth828

    Joined:
    Feb 8, 2020
    Posts:
    12
    Did you watch the full video? Also, the rotation seems to be going out of the random range bounds. If you watch the video, you see that eventually, the whole thing just goes all corrupt.
     
  7. Moogamouth828

    Moogamouth828

    Joined:
    Feb 8, 2020
    Posts:
    12
    Also, just wait, I will try your code.
     
  8. Moogamouth828

    Moogamouth828

    Joined:
    Feb 8, 2020
    Posts:
    12
    I can't right now.
     
  9. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    First of all, your range is 0 to 360 on all axis. That means that "out of bounds" literally does not exist, as you include all possible values. Not to mention you later overwrite it with different values, meaning the range is irrelevant, more on that below.

    Secondly "transform.rotation" contains a Quaternion value. That's a 4-dimensional complex number value used to describe rotations in three dimensional space. Its x, y, z and w (yes, w) values do not contain what you think they do. Changing them or setting a rotation to some negated value of them does not what you think it does - even tho i'm not quite sure what you are attempting to do anyways. As a general note, Unity uses these quaternion values internally. Euler angles are mostly there for us to get an easier understanding of an angle. A single quaternion value can be represented by different euler angles tho, so that can cause some confusion.

    Quite frankly, your solution to whatever problem you are trying to solve looks wierd. This is often the case when someone does not understand a problem X, tries to solve it with some solution Y, and then asks about help on Y, which results in weird and hard to answer questions.
    This is commonly known as the XY problem. http://xyproblem.info/
    Do you mind explaining what it actually is you are trying to do?
     
    Last edited: Nov 8, 2020
    Bunny83 and Antypodish like this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Are you just trying to shake the camera? If so there's like around 27,000 Youtube tutorials on how to do that in Unity. I suggest you start there.
     
    Bunny83 likes this.
  11. Moogamouth828

    Moogamouth828

    Joined:
    Feb 8, 2020
    Posts:
    12
    Yeah, kind of. I basically want the camera to just go insane. I'll look for that on youtube.
     
  12. Moogamouth828

    Moogamouth828

    Joined:
    Feb 8, 2020
    Posts:
    12
    Ok, so for everyone who's confused about my intentions, I basically want the camera to have an infinite seizure, and just continuously set its rotation and position to random values. Very simple project, but I'm a beginner, so.
     
  13. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,579
    Your little exercise:
    Please explain your reasoning, for line 12 and 13. Why they are there and what should they do.
    Please explain step by step, what is happening there.

    Copping line 12 and 13.
    Code (CSharp):
    1. transform.Rotate(new Vector3(-transform.rotation.x, -transform.rotation.y, -transform.rotation.z));
    2. transform.Translate(new Vector3(-transform.position.x, -transform.position.y, -transform.position.z));
    3.  
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Note: the x,y,z components of a quaternion such as transform.rotation are not what you think they are.

    Perhaps you're looking for the .eulerAngles.x (and .y and .z) ?

    Still no idea what you're doing with line 12 though.

    Note for the desired shaking effect: tiny random movements of camera angle (think 1 or 2 degrees either way) result in extremely jarring feelings. You're making massive movements which will just feel like the camera freaking out, as you note.
     
  15. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    81
    You should remove the last two lines. They don't seem to be of any use.
    And set the translation range to smaller number.


    Code (CSharp):
    1. using UnityEngine;
    2. public class ShakeEffect : MonoBehaviour
    3. {
    4.  
    5.    
    6.     void Start()
    7.     {
    8.      
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         transform.Rotate(new Vector3(Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360)));
    14.         transform.Translate(new Vector3(Random.Range(0,1 ), Random.Range(0 ,1 ), Random.Range(0 , 1 )));
    15.        
    16.     }
    17.  
    18. }
     
    Bunny83 likes this.
  16. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,527
    Yep, that's spot on. The main issue is the last line. Since Rotate as well as Translate work with relative local space values, passing the worldspace position as a relative translation will translate you in an arbitrary direction (depending on the current rotation). As a result your new position will be either about the same distance from the origin or in the worst case root 2 (about 1.414) times further away. In the majority of cases your distance will grow. That leads to an overall exponential growth of your distance from the origin. That's why, after a few seconds, your position is getting way too large and you're outside of a stable range. Unity displays a warning in the inspector.

    Feel free to check the actual position values at runtime. Just press pause and make the inspector window wider. Your position values are already too large so they are displayed in scientific format and the exponent is currently cut off at the end..
     
  17. Moogamouth828

    Moogamouth828

    Joined:
    Feb 8, 2020
    Posts:
    12
    Because transform adds the value to the movement, instead of setting it, like I wanted.
     
  18. Moogamouth828

    Moogamouth828

    Joined:
    Feb 8, 2020
    Posts:
    12
    How do I stop this, though?
     
  19. Moogamouth828

    Moogamouth828

    Joined:
    Feb 8, 2020
    Posts:
    12
    That's just what I want. Except for the fact that the position and rotation of the camera go past the random ranges, and the whole thing glitches out at the end.
     
  20. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,579
    That is correct.
    And here is you problem.
    Because for your script to work, you would need always make camera start at 0 position.
    Otherwise, error will accumulate over the time.
    Your random does not guarantee, the average random over the time will be 0.
    It is most likely be on one side of the spectrum. Meaning, you increase your error.

    You should probably better off using setting position, rather adding. But you need store position before shaking happening and use it as reference, until is finished.

    Other than that, yes look into existing solutions and try learn from them.
     
    Bunny83 likes this.
  21. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,527
    Exactly. If I would create such a (crazy) component I would probably do something along the lines:

    Code (CSharp):
    1.  
    2. public class Seizure : MonoBehaviour
    3. {
    4.     Vector3 startPos;
    5.     Quaternion startRot;
    6.     void OnEnable()
    7.     {
    8.         startPos = transform.position;
    9.         startRot = transform.rotation;
    10.     }
    11.     void OnDisable()
    12.     {
    13.         // uncomment the following lines if you want to move the camera back once done.
    14.         //transform.position = startPos;
    15.         //transform.rotation = startRot;
    16.     }
    17.     void Update()
    18.     {
    19.         transform.Rotate(new Vector3(Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360)));
    20.         transform.position = startPos + new Vector3(Random.Range(-100f, 100f), Random.Range(-100f, 100f), Random.Range(-100f, 100f));
    21.     }
    22. }
    23.  
    Another option would be a coroutine. Though when we encapsulate this functionality in a seperate class like this there's no point in adding this complexity. This class can simply be enabled / disabled to enable or disable the effect.