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!
Answer by Baby_Dino · Nov 25, 2015 at 05:09 PM
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
Thanks for the reply.
The Translate code kind of worked, thanks, (and i donno why .velocity is not working), but the problem is .Translate moves the object as if it's "cutting and pasting" the object at a different position -- not a smooth move, rather like a jump. So I need to press the button continuously to make the ship 'hop' at least. I donno if .velocity is working as same as this -- it never worked for me yet. What I want is to move the object smoothly when I press and hold the button. Can .Translate do that if I implement it inside a "press and hold" button event -- wouldn't it still 'hop' as I hold the button -- I think it will.
Also I don't know how to implement a "press and hold" button event. Could you please shortly describe how to do that? -- so I could put your code inside it and test how the object move.
$$anonymous$$asun
Ok, here is how you use velocity in Unity5: GetComponent().AddForce(transform.right * 30); See if that works, if it doesn't, I will look into how to implement a "press and hold" button event (I don't use buttons in my games, I use the arrow keys, mouse, etc. so I don't know much about how they work yet).
Hi
Actually i've implemented addForce earlier(which also not worked at all), like this:
GetComponent<Rigidbody>().AddForce(movemnt * speed);
Your code for addForce returned an error:
The type arguments for method `UnityEngine.Component.GetComponent<T>()' cannot be inferred from the usage.
btw, with $$anonymous$$B key's the code works pretty fine. It's when i implemented the code with a button that this bug appear. (weird ha. ???)
I'll try to find out how to implement "press and hold".
And, Thanks for continues support :)