• 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 /
avatar image
0
Question by AmirHDxC · Jun 07, 2015 at 09:47 AM · physics

Script doesn't move object in Unity 5

I had a script in unity 4, which with mouse click, spawns triangles and push them towards a circle. Now in Unity 5, nothing happens. No errors, it just spawns triangles and then nothing.

 #pragma strict
 var triangle : GameObject;
 var circle : GameObject;
 
 function Start () {
 
 }
 
 function Update () {
     var mouseStart : Vector2;
     var mouseStop : Vector2;
     var powerVector : Vector2;
     if( Input.GetMouseButtonDown(0) ){
         mouseStart = Input.mousePosition;
     }
     if( Input.GetMouseButtonUp(0) ){
         mouseStop = Input.mousePosition;
         powerVector = mouseStop - mouseStart;
         var a : Vector2 = Camera.main.ScreenToWorldPoint(mouseStop);
         var hit1 : RaycastHit2D = Physics2D.Raycast(a, Vector2.up);
         var hit2 : RaycastHit2D = Physics2D.Raycast(a, Vector2.right);
         if ( (hit1.collider == null) || (hit2.collider == null) ){
             var copyTriangle : GameObject = Instantiate(triangle,a , Quaternion.identity);
             copyTriangle.GetComponent.<Rigidbody2D>().AddForce(powerVector, ForceMode2D.Force);
         }
     }
 }
Comment
Add comment · Show 3
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 Crystalline · Jun 07, 2015 at 10:36 AM 1
Share

Put some debug lines here n there just to see if things even get called.

avatar image AmirHDxC · Jun 08, 2015 at 06:44 AM 0
Share

Code is executed pretty well until the AddForce part. The triangle is spawned, but its not moving.

avatar image Wolfdog · Jun 08, 2015 at 07:05 AM 0
Share

The force is probably too small. Try Debug.Log (powerVector) to see your force. If it's something like (50, 50), multiply if to be around (500, 500).

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Propagant · Jun 08, 2015 at 07:10 AM

In this line

  if ( (hit1.collider == null) || (hit2.collider == null) ){
              var copyTriangle : GameObject = Instantiate(triangle,a , Quaternion.identity);
              copyTriangle.GetComponent.<Rigidbody2D>().AddForce(powerVector, ForceMode2D.Force);
          }

and the specific line

  copyTriangle.GetComponent.<Rigidbody2D>().AddForce(powerVector, ForceMode2D.Force);

there is no force parameters! You must add force like:

 AddForce(powerVector * myforce, ForceMode2D.Force);

got it?

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 Wolfdog · Jun 08, 2015 at 07:13 AM 0
Share

And don't forget to also multiply it by Time.deltaTime if it's in the Update() function.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D 360 degress platformer example needed 0 Answers

ignore forces except gravity? 1 Answer

How to make a system that will let elements interact with other elements? 1 Answer

Keeping characters within Fighting area 1 Answer

Can I stop a lerp completely when it gets to 20% of its original value? 3 Answers


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