• Advertisement
Sign in to follow this  

Move objects left and right with Unity 2d

This topic is 401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a car object I wrote the codes in the picture to move it but it does not move I can not find anything about it on the internet what do I do

Share this post


Link to post
Share on other sites
Advertisement

Can you paste a screenshot of the script in the Inspector? One of your multiplication floats is probably valued at 0 resulting in a 0-force.

Share this post


Link to post
Share on other sites

It is likely to either be that those public values are set as zero in the inspector for that GameObject. Or (assuming that the GetComponent() is being called and returns right), that the object weighs so much that you need more force.

 

Of course the Rigidbody itself may be constrained either by axis or literally in the world too by obstacles. 

Edited by Josh Green

Share this post


Link to post
Share on other sites

First and foremost, you're using a Vector3 for a 2d rigidbody object. I'm surprised that that didn't cause a straight up error somewhere, considering the API says that it should be a Vector2: 

https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html

 

Change the Vector3.right in addforce line to Vector2.right and see if it works. 

 

Vector3 is implicitly cast to Vector2 (z is discarded). Vector2 cannot implicitly cast to Vector3 though, if I recall correctly.

Share this post


Link to post
Share on other sites

I have a car object I wrote the codes in the picture to move it but it does not move I can not find anything about it on the internet what do I do


Make sure your script is actually attached to the correct GameObject (I know this sounds silly but you haven't told us what you've done).

Put a Debug.Log in that function and print out all those variables separately, and find out if any of them aren't doing what you expect.

Check your Rigidbody2D in the inspector to see if it's got any values that might be causing problems: high mass, high drag, wrong body type, freeze position constraint, etc. Edited by Nypyren

Share this post


Link to post
Share on other sites

 

First and foremost, you're using a Vector3 for a 2d rigidbody object. I'm surprised that that didn't cause a straight up error somewhere, considering the API says that it should be a Vector2: 

https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html

 

Change the Vector3.right in addforce line to Vector2.right and see if it works. 

 

Vector3 is implicitly cast to Vector2 (z is discarded). Vector2 cannot implicitly cast to Vector3 though, if I recall correctly.

 

 

Well then that's more or less out then.

 

My recommendation would be to output each variable to the console and see what's zero in that multiply. 

 

EDIT: Nypren gives good advice. Make sure you've attached it and attached it to the right object. I'd also recommend you make sure you're checking the right input (i.e. which axis corresponds to which keys and if that's what you wanted). That may sound silly, but sometimes silly things like that can be the cause of issues.

Edited by deltaKshatriya

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement