hidonut123
hidonut123

Reputation: 11

How to make script only translate on one axis?

I would like my sprite to move only on axis, so I took out the x-axis from the code. However, when I try to compile it in Unity, it returns with this error:

Assets/Scripts/TongueMove.js(19,83): BCE0024: The type 'UnityEngine.Vector2' does not have a visible constructor that matches the argument list '(float)'.

Furthermore, what would I add so that it only lasts a certain time, before returning to its original position?

#pragma strict

function Start() {
    transform.position.z = -0.5;
}

function Update () {
    if (Input.GetKeyDown ("space"))
    {
        moveTo(transform.position.y + 11.8, 20); //transform.position.y + how much I want to move, speed
    }
    transform.position.z = -0.5;
}

function moveTo (posY : float, speed : float)
{
    while (transform.position.y != posY)
    {
        transform.position = Vector2.MoveTowards (transform.position, new Vector2(posY), speed * Time.deltaTime);
        yield;
    }
}

Upvotes: 0

Views: 147

Answers (1)

Laura Abad Avilés
Laura Abad Avilés

Reputation: 786

Your problem is when you create the new Vector2() into the while loop. Vector2 needs 2 parameters.

If you don't like to modify X axis try this:

while (transform.position.y != posY)
{
 transform.position = Vector2.MoveTowards (transform.position, new Vector2(transform.position.X, posY), speed * Time.deltaTime);
yield;

http://docs.unity3d.com/ScriptReference/Vector2.html

Upvotes: 1

Related Questions