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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[unsolved] improving script and help rotation

Discussion in 'Scripting' started by Jorjor210, Jun 7, 2015.

  1. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    First off, I recently made a script that allow my enemy to bounce off the wall at about a 45 degree angle, and although I made work (with the exception to whats said below the script) I'm sure there's a much simpler and more logical way of doing the same thing. my script:
    Code (CSharp):
    1.  MonoBehaviour {
    2.         public Vector3 currentDirection;
    3.     public Vector3 currentDirection2;
    4.         public float Speed = 1f;
    5.         void Start()
    6.         {
    7.         currentDirection = Vector3.down;
    8.         currentDirection2 = Vector3.right;
    9.         }
    10.      
    11.         void Update()
    12.         {
    13.             transform.Translate(currentDirection *Time.deltaTime * Speed);
    14.         transform.Translate(currentDirection2 *Time.deltaTime * Speed);
    15.         }
    16.         void OnCollisionEnter2D (Collision2D col)
    17.         {
    18.             if (col.gameObject.name == "Floor") {
    19.             int i = Random.Range (0,3);
    20.             if(i<=1){
    21.                 currentDirection = Vector3.up;
    22.                 currentDirection2 = Vector3.right;
    23.             }
    24.             if(i==2){
    25.                 currentDirection = Vector3.up;
    26.                 currentDirection2 = Vector3.left;
    27.             }
    28.                 transform.Translate (currentDirection * Time.deltaTime * Speed);
    29.             transform.Translate (currentDirection2 * Time.deltaTime * Speed);
    30.             }
    31.             if (col.gameObject.name == "Ceiling") {
    32.             int x = Random.Range (0,3);
    33.             if(x<=1){
    34.                 currentDirection = Vector3.down;
    35.                 currentDirection2 = Vector3.right;
    36.             }
    37.             if(x==2){
    38.                 currentDirection = Vector3.down;
    39.                 currentDirection2 = Vector3.left;
    40.             }
    41.                 transform.Translate (currentDirection * Time.deltaTime * Speed);
    42.             transform.Translate (currentDirection2 * Time.deltaTime * Speed);
    43.             }
    44.         }
    45.     }
    46.  
    Also I would like my enemy to slowly rotate in the Z direction. And I have the script for this and it works 100%. But due to this script and the other script both being on the same object. when the object goes around 180 degrees in the z direction. up is then down. and down is then up (They get reversed due the object rotating) does anyone have an idea how to improve this script to allow rotating in the z direction?
     
  2. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    Anyone?
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    To fix the rotation issue, just always move relative to the world rather than local space by replacing your transform.Translates() with this:
    Code (csharp):
    1.  
    2. transform.position += currentDirection * Time.deltaTime * Speed;
    3. transform.position += currentDirection2 * Time.deltaTime * Speed;
    4.  
    You can optimize your collision code just by restructuring it to remove duplicate code:
    Code (csharp):
    1.  
    2. void OnCollisionEnter2D (Collision2D col)
    3. {
    4.    int direction = Random.Range(0, 3);
    5.  
    6.    if(col.GameObject.name == "Floor")
    7.    {
    8.      currentDirection = Vector3.up;
    9.      currentDirection2 = (direction <= 1) ? Vector3.right : Vector3.left;
    10.    }
    11.    else if(col.GameObject.name == "Ceiling")
    12.    {
    13.      currentDirection = Vector3.down;
    14.      currentDirection2 = (direction <= 1) ? Vector3.right : Vector3.left;
    15.    }
    16.  
    17.    transform.position += currentDirection * Time.deltaTime * Speed;
    18.    transform.position += currentDirection2 * Time.deltaTime * Speed;
    19. }
    20.  
    If you've never seen a statement like (direction <= 1) ? Vector3.right : Vector3.left it's called a Ternary operation and you can think of it as an inline if-else statement.
     
    Jorjor210 likes this.
  4. Maximillion

    Maximillion

    Joined:
    Dec 20, 2013
    Posts:
    22
    On transform.Translate set the relativeTo to Space.World, see http://docs.unity3d.com/ScriptReference/Transform.Translate.html:

    Code (CSharp):
    1. transform.Translate(Movement Amount, Space.World)
    This should stop the rotation being taken into consideration at all and use the world xyz.

    As for tidying up the script, you should use "else ifs" and not include so many transform.Translates (if it is in update it shouldn't need to be in OnCollisionEnter2D.

    You may also be able to do it with only one currentDirection and do it like this:

    Code (CSharp):
    1. currentDirection = Vector3.down + Vector3.left
    This may be a better way of doing it.
     
  5. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    ahhh very helpful thanks :)