Object Movment problem (3D)

Hi all,

I’ve attempted creating 2 games with Unity so far – one is 2D other is 3D. In my 2D game, i haven’t had any problems with moving objects.

Here’s my code to move my player object to left, on left UI button Click:

float moveSpeed = 2f;
.......
.......
public void SheWalksL()
{
     AniWalkL (); // Animation

     GetComponent<Rigidbody2D> ().velocity = new Vector2 (-moveSpeed, GetComponent<Rigidbody2D> ().velocity.y); // Move Left
}

Above code is working on UI Button press. But in my 3D game, when i try to move my player object in the same way (with the added 3rd dimension), it’s not working. Here’s the code:

public void ButtonMoveL()
{
  Vector3 movemnt = new Vector3(-2.0f, GetComponent<Rigidbody>().velocity.y, 0);
  GetComponent<Rigidbody>().velocity = movemnt * speed; // Move Left (Vantage point: looking down at the scene, along the Y-axis. So, Z is aligned vertically and X is aligned horizontally)
}

Only difference is that in the 3D game, i’ve created the movement vector separately and assigned it to the object velocity, and provided the X value of the vector directly without using a variable. I’ve also got a “Fire” button, which functions perfectly when pressed, so it can’t be a problem with the button event call. Could someone please point out what i’ve done wrong in the 3D code to make it to not working.

Thanks for any input!

@KasunL:

Hmmmm… I plugged your movement code into my own 3d game that I am creating and it worked, check to make sure that you have a Rigidbody (not Rigidbody2d) attached to your game object and double-check to make sure that the function is actually being called.

By the way, this is usually a better way to move an object in 3d:
transform.Translate(Vector3.left * Time.deltaTime*speed);
that will move the object that is attached to the script to the left. You should be able to put that in your function and make the object move (assuming that the function is called).

Tell me if that works