Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by 1o482013581451 · Oct 28, 2016 at 04:41 PM · c#movementaddforcemovement script

What is wrong with this script?

This is my 5th augmentation of some code to make a cube move. That's it, just move in any direction at any speed and I have been ripping my hair out for 2 days trying to get it to work. I have copied and pasted Unity's own, 2016 updated code and it doesn't even come close to working. This is my best variation with what I think is the most comprehensible error message... \\ CS1501: No overload for method 'AddForce takes '4' arguments \\ I've looked it up and it looks like it happens when you improperly call a variable although I don't see where this is happening. I even removed all the variables including void Move() and it still gave me that error. Here is the code...

 using UnityEngine;
 using System.Collections;

 public class SimpleMovement : MonoBehaviour {

 public float thrust = 10;
 public Rigidbody2D rb2D;
 void Start () 
 {
     rb2D = GetComponent<Rigidbody2D> ();
 }

 void Move () 
 {
     rb2D.AddForce(0, thrust, 0 , ForceMode.Impulse);
 }

 void Update() 
 {
     if(Input.GetKeyDown(KeyCode.Space))
         Move();
     Debug.Log ("I hate C#");
 }

 }

And please, don't just copy and paste Unity Docs in a lazy response, there is nothing left for me there...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by OutOfRam · Oct 28, 2016 at 05:04 PM

Hey there,

This should be pretty simple, the first argument that you have set to 0 in the addForce function is an int value, but the function requires a vector2 value. This means it requires both an x and y value. Also you only need the force and force mode, it should only be 2 arguments try this;

 public Vector2 thrust;
 public Rigidbody2D rb2D;
 void Start () 
 {
     thrust = new Vector2(1,4);//thrust will be 4 times as strong in the y af it is in the x
     rb2D = GetComponent<Rigidbody2D> ();
 }
 void Move () 
 {
     rb2D.AddForce(thrust, ForceMode2D.Impulse);
 }
 void Update() 
 {
     if(Input.GetKeyDown(KeyCode.Space))
         Move();
     Debug.Log ("I hate C#");
 }

I hope this helps!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 1o482013581451 · Oct 28, 2016 at 05:20 PM 0
Share

Thanks, that clears things up...

avatar image
0

Answer by Zynek · Oct 28, 2016 at 04:55 PM

Please read the documentation a little more carefully https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html

The method takes 2 arguments: Vector2 force, ForceMode2D mode = ForceMode2D.Force

"0, thrust, 0" is not a Vector2 !!!

Here´s how you define a Vector2:

 new Vector2(x,y);

where x and y are float variables that specify direction in x,y axis.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by tiggy02 · Oct 28, 2016 at 05:16 PM

Your problem is that you are not specifying that you are moving in a Vector2 you are trying to move in a Vector3 with specifying that it is a Vector3.

void Move () { rb2D.AddForce(new Vector2(thrust, 0), ForceMode.Impulse); }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

257 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an object go the direction it is facing? (Im new) 0 Answers

Face direction of a Vector 3 1 Answer

Input.GetAxisRaw always returns -0.6 1 Answer

[I still need help even though there is 2 replies] Help with shooter script 3 Answers

Switching lanes 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges