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!