[C#] Vector2.up

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.

using UnityEngine;
using System.Collections;

public class SmasherScript : MonoBehaviour {

    public GameObject smasherBottom;
    public GameObject smasherTop;
    public bool isUp = false;
    public int SmashSpeed;



    void OnMouseDown(){
        Debug.Log ("MouseDown detected");
        if (isUp == false) {
            smasherBottom.transform.Translate (Vector2.up * SmashSpeed);
            smasherTop.transform.Translate (Vector2.up * -SmashSpeed);
            isUp = true;

        }
    }


    void OnMouseUp(){
        Debug.Log ("MouseUp detected");
        if (isUp == true) {
            smasherBottom.transform.Translate (Vector2.up * -SmashSpeed);
            smasherTop.transform.Translate (Vector2.up * SmashSpeed);
            isUp = false;;
            }

        }

    }

Try

smasherBottom.transform.Translate (Vector2.up * -SmashSpeed, Space.World);
smasherTop.transform.Translate (Vector2.up * SmashSpeed, Space.World);

Thank you! It worked, but I don’t understand why. Could you please explain?

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.

You could also use Unity - Scripting API: Transform.TransformDirection. Also you should multiply your code by Time.deltaTime so it’s framerate independent :slight_smile: