• 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
Question by Flimzy · Dec 16, 2022 at 03:46 PM · errorrigidbody2dbeginnerglitchrigidbody.addforce

rb.AddForce() is launching my player in the opposite direction of the vector 2 value I assigned

I'm making a game where you click and drag with the mouse then release to launch the player. But sometimes the player gets launched in the opposite direction of where it should go. I made a debug output to show you the different values. Here is the output

In that image for example you can see that the Vector2 of force * power is positive on the y-axis, but the player launched downwards, and the same happens Vice versa. I think it's also worth noting that this happens inconsistently for some reason. Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour
 {
     public GameObject player;
     public float power = 10f;
     public Rigidbody2D rb;
     public float maxSpeed;
 
     public Vector2 minPower;
     public Vector2 maxPower;
 
     TragectoryLine tl;
 
     Camera cam;
     public Vector2 force;
     public Vector3 startPoint;
     public Vector3 endPoint;
     public Vector3 currentPoint;
     public Vector3 startPointMouse;
 
     public bool isPulling = false;
     float distance;
 
 
 
     private void Start()
     {
         cam = Camera.main;
         tl = GetComponent<TragectoryLine>();
     }
 
     private void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             startPointMouse = cam.ScreenToWorldPoint(Input.mousePosition);
             startPointMouse.z = 15;
         }
         if (Input.GetMouseButton(0))
         {
             startPoint = player.transform.position;
             startPoint.z = 15;
             isPulling = true;
             Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
             currentPoint.z = 15;
             tl.RenderLine(startPoint, currentPoint);
 
           
         }
 
         if (Input.GetMouseButtonUp(0))
         {
             endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
             endPoint.z = 15;
             isPulling = false;
             
             tl.EndLine();
             distance = startPointMouse.magnitude - endPoint.magnitude;
 
             if (distance < 0)
             {
                 distance = -distance;
             }
             if (distance >= 1)
             {
                 rb.AddForce(force * power, ForceMode2D.Impulse);
             }
 
 
             force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
 
             Debug.Log("distance" + distance);
             Debug.Log("start" + startPoint);
             Debug.Log("end" + endPoint);
             Debug.Log("force" +force);
             Debug.Log("force * power" + force * power);
         }
 
     }
 
     private void FixedUpdate()
     {
         rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
     }
 }


Here I added the force using rb.AddForce(force power, ForceMode2D.Impulse); when the force power value was positive on the y axis. So why did it go to the opposite direction???

This was working perfectly fine before i tried implementing a feature where the player has to move the mouse a certain distance or else it wont launch. I have tried removing it but it doesnt seem to make a difference. I think I changed something in the code that ruined it but I cant figure out what! Please help!

error.png (16.3 kB)
Comment

People who like this

0 Show 0
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

Answer by ojaoweir · Dec 17, 2022 at 09:23 AM

You calculate the force after it has already been added. So my guess is that it is using the force of your previous drag. Right now the scripts does things in the following order:


  1. Adds force

  2. Calculates the new force

  3. Writes force into console


While the force you write in the console is the one you have calculated, it is not the force you have added to your rigid body.

Move the calculation of force to before you do rb2D.AddForce

Comment

People who like this

0 Show 0 · 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

212 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

Related Questions

"The type or namespace name 'PlaceableObject' could not be found..." Tutorial is failing and I am a *beginner* beginner!!! SEND HELP 0 Answers

Rigidbody.addforce not working with prefab 1 Answer

Do rigid bodies with force add force to other objects? 0 Answers

UCE0001 and BCE0044 Errors 1 Answer

In order to call GetTransformInfoExecectUpToDate 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