Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by Lunier · Mar 09, 2017 at 05:30 PM · rigidbodyaddforcemoveposition

myRigidbody.MovePosition influences the force.

Hello,

somehow this part of my Code:

myRigidbody.MovePosition(targetPosition);

influences this one:

 if (Input.GetMouseButtonDown(0))
         {
             myRigidbody.velocity = Vector2.zero;
             myRigidbody.AddForce(new Vector2(0, upforce));
         }

in that way that the added force isnt as big it was without the moveposition code. Can someone help me?

Comment
Add comment · Show 5
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 hexagonius · Mar 09, 2017 at 05:52 PM 0
Share

can you rephrase, I don't understand your sentence

avatar image Lunier hexagonius · Mar 09, 2017 at 05:58 PM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NewBehaviourScript : $$anonymous$$onoBehaviour
 {
 
     public Rigidbody2D myRigidbody;
     public Camera cam;
     private float maxWidth;
     private bool isGrounded;
     private float upforce = 200f;
 
     void Start()
     {
         //Screen.sleepTimeout = SleepTimeout.NeverSleep;
         myRigidbody = GetComponent<Rigidbody2D>();
         if (cam == null)
         {
             cam = Camera.main;
         }
         
         Vector2 upperCorner = new Vector2(Screen.width, Screen.height);
         Vector2 targetWidth = cam.ScreenToWorldPoint(upperCorner);
         float ballWidth = GetComponent<Renderer>().bounds.extents.x;
         maxWidth = targetWidth.x - ballWidth;
     }
     // Update is called once per frame
     void FixedUpdate()
     {
 
         Vector2 rawPosition = cam.ScreenToWorldPoint(Input.mousePosition);
         Vector2 targetPosition = new Vector2(rawPosition.x, transform.position.y);
         float targetWidth = $$anonymous$$athf.Clamp(targetPosition.x, -maxWidth, maxWidth);
         targetPosition = new Vector2(targetWidth, targetPosition.y);
         myRigidbody.$$anonymous$$ovePosition(targetPosition);
 
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             myRigidbody.velocity = Vector2.zero;
             myRigidbody.AddForce(new Vector2(0, upforce));
         }
 
         //myRigidbody.AddForce(Vector2.down * 100);
         
         
     }

Thats my whole code yet. With the line of code to move the object so:

myRigidbody.$$anonymous$$ovePosition(targetPosition);

somehow the object doesnt get the force correctly applied to it.

avatar image hexagonius Lunier · Mar 09, 2017 at 06:36 PM 0
Share

ok, doing a move on two axis while trying to apply forces on one or two of the same axis just cannot work. You need to decide if it's a complete axis you want to ignore (like the y axis cause it should be influenced by forces), or if you just calculate everything together and set that as the rigidbodies velocity.
A sidenote to that. Setting velocity is the savest you can do. I tried with $$anonymous$$ovePosition once and it's just not a good way to move a rigidbody. While $$anonymous$$ovePosition complies with the interpolation settings set, it doesn't comply with collision (tried it once, just glitched through a wall).
Bottom line. I would calculate the velocity on the x axis for the mouseposition and leave y as is. Or do some fancy calculation with y so it's half the mouse click and half the force.

Show more comments
avatar image hexagonius · Mar 11, 2017 at 07:57 AM 0
Share

mouse position $$anonymous$$us position (both in world space), normalize times speed. that's the velocity in unit per second towards the target (overshooting).

0 Replies

· Add your reply
  • Sort: 

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

110 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

Related Questions

RigidBody Addforce While using Moveposition 0 Answers

Rigidbody Addforce not working as expected ( video included) 0 Answers

Character jumping too fast 1 Answer

Terminal velocity on x and y axis is higher than normal when force is applied to both axis simultaneously. 0 Answers

Rigidbody AddForce() stops working after walking away for a while 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