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

[C#] Vector2.up

Discussion in 'Scripting' started by GreenBoxInteractive, Apr 25, 2015.

  1. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    I have a script that is trying to make to things appear as smashing together, I have done it by haveing smasherBottom.Transform.Translate (Vector2.up * SmashSpeed) for the bottom part, as expected it moves up.

    Then for the top arm, I thought just doing the same but making the number of SmashSpeed negative. smasherTop.Transform.translate(Vector2.Up * -SmashSpeed) but the problem is, the top the smasherTop does the same movement as smasherBottom.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SmasherScript : MonoBehaviour {
    5.  
    6.     public GameObject smasherBottom;
    7.     public GameObject smasherTop;
    8.     public bool isUp = false;
    9.     public int SmashSpeed;
    10.  
    11.  
    12.  
    13.     void OnMouseDown(){
    14.         Debug.Log ("MouseDown detected");
    15.         if (isUp == false) {
    16.             smasherBottom.transform.Translate (Vector2.up * SmashSpeed);
    17.             smasherTop.transform.Translate (Vector2.up * -SmashSpeed);
    18.             isUp = true;
    19.  
    20.         }
    21.     }
    22.  
    23.  
    24.     void OnMouseUp(){
    25.         Debug.Log ("MouseUp detected");
    26.         if (isUp == true) {
    27.             smasherBottom.transform.Translate (Vector2.up * -SmashSpeed);
    28.             smasherTop.transform.Translate (Vector2.up * SmashSpeed);
    29.             isUp = false;;
    30.             }
    31.  
    32.         }
    33.  
    34.     }
    35.  
     
  2. GGeff

    GGeff

    Joined:
    Nov 11, 2014
    Posts:
    40
    Try
    Code (CSharp):
    1. smasherBottom.transform.Translate (Vector2.up * -SmashSpeed, Space.World);
    2. smasherTop.transform.Translate (Vector2.up * SmashSpeed, Space.World);
     
  3. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Thank you! It worked, but I don't understand why. Could you please explain?
     
  4. GGeff

    GGeff

    Joined:
    Nov 11, 2014
    Posts:
    40
    Without specifying the space, it uses the object's local space, and I am guessing you have one of the objects rotated 180 degrees so that it is facing downwards.
     
  5. akitsu

    akitsu

    Joined:
    Nov 26, 2013
    Posts:
    38