Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by Flumph1 · Feb 11 at 08:32 AM · addforce2d-physics

AddForce Impulse not moving the object

Hi, I have a prefab object called "Meteor", which is instantiated randomly at the right edge of the screen using a coroutine. The meteor is instantiated through another script, named "meteorController", to prevent clones from instantiating themselves. The script is attached to a gameobject with the same name. After MeteorPrefab is instantiated, I want to give it force towards another object, called "Spaceship". I've tried doing it using AddForce with Impulse as the force mode, but it's not working: the meteors are instantiated properly but they aren't moving at all, although no error messages are displayed. The MeteorPrefab's rigidbody is set to "dynamic" and it has a mass value of 1. I'm new to #c. maybe something is wrong with my code? Help would be appreciated. Thanks

The meteorController script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class meteorController : MonoBehaviour
 {
     public GameObject MeteorPrefab;
     public float timer = 5.0f;
     private Vector2 screenBounds;
     public Transform spaceship;
     public Rigidbody2D rb;
     public float force = 100.0f;
 
     void Start()
     {
         screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
         StartCoroutine(Meteor());
         Debug.Log(screenBounds);
     }
 
 
 
     private void SpawnMeteor()
     {
         GameObject spawn = Instantiate(MeteorPrefab) as GameObject;
         spawn.transform.position = new Vector2(screenBounds.x, Random.Range(-screenBounds.y, screenBounds.y));
         rb = spawn.GetComponent<Rigidbody2D>();
         Vector3 direction = spaceship.position - transform.position;
         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
         rb.rotation = angle;
         rb.AddForce(spawn.transform.forward * force, ForceMode2D.Impulse);
     }
 
     IEnumerator Meteor()
     {
         while (true)
         {
             yield return new WaitForSeconds(timer);
             SpawnMeteor();
         }
     }
 
 
 }
 

Here's a screenshot of the MeteorPrefab's Rigidbody2D component:

alt text

screenshot-2022-02-11-102405.png (49.3 kB)
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

1 Reply

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

Answer by Eno-Khaon · Feb 11 at 11:05 AM

Since your game's in 2D, using a 3D, z-axis vector for movement shouldn't be able to cause visible movement.

 rb.AddForce(spawn.transform.forward * force, ForceMode2D.Impulse);

I wouldn't know your intended orientation off hand, but using something like Transform.right or .up should be all that's necessary to apply force in a usable direction:

 // For reference, a variation that ignores mass, in case
 // you change that value eventually
 rb.AddForce(rb.mass * spawn.transform.right * force, ForceMode2D.Impulse);
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 Flumph1 · Feb 11 at 12:27 PM 0
Share

It works now, thank you!

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

142 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

Related Questions

Moving player to the opposite direction of the mouse position 1 Answer

Glider physics with AddForce 2D 1 Answer

AddForce to x axis when mouse position is close to x axis ? And if closer to x axis the more addforce 0 Answers

Bouncing Toward Target Point 0 Answers

Acceleration and Max Speed on 2D Object 0 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