Advertisement

Move objects left and right with Unity 2d

Started by March 14, 2017 09:24 PM
7 comments, last by warhound 7 years, 8 months ago

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

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.

Advertisement

Maybe hiz is 0?

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

speed 10

Maybe hiz is 0?

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.

Learn to create games through our vibrant platform of easy-to-follow video training material. Getting recognition of the knowledge and skills you gain from the courses is essential. We can provide you with globally recognised City & Guilds accredited certificates.

www.polygoncollege.com

EDIT: The Vector3 and Vector2 thing shouldn't be an issue. Disregard.

No one expects the Spanish Inquisition!

Advertisement

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.

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.

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.

No one expects the Spanish Inquisition!

This topic is closed to new replies.

Advertisement