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 /
avatar image
0
Question by simone84 · May 28, 2011 at 12:45 PM · javascriptmovementaddforcescreentoworldpointperspective

addforce + persp camera movement weirdness

I'm using mouse position to control the movement of an object on screen, basically wherever you click pushing the object in the opposite direction and how long you hold the mouse down for the speed.

Mostly it's fine, but every few clicks or seconds it comes back with the wrong direction pulling the object toward the mouse instead of pushing it away.

Using an ortho camera instead of persp seems to work fine, so it's something to do with that but I can't figure out how to fix it. And turning off the oscillation doesn't help.

Here's my code:

    function Start()    {
     
         particleEmitter.emit = false;
         yield WaitForSeconds(5);
         particleEmitter.emit = true;
     }
     
     
     function Update () {
         
         if(Input.GetButtonDown("Fire1"))    {
         
             clickStart = Time.time;
             mousePos = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z)); 
             currentPos = Vector3(transform.position.x, transform.position.y, transform.position.z);
             
             moveDir = mousePos - currentPos;        // persp camera, change for orth
             moveDir.z = 0;                                //don't move the object on the z axis
             moveDir.Normalize();
         }
     
         if(Input.GetButtonUp("Fire1"))    {
     
             clickEnd = Time.time;
             
             //speedCalc = Vector3.Distance(mousePos, currentPos);        //distance pointer from seed to set the speed
             speedCalc = clickEnd - clickStart;                        //time mouse button held to set the speed
             
             vel = Mathf.Clamp((speedCalc / moveSpd), minVel, maxVel);
             rigidbody.AddForce(moveDir * vel, ForceMode.Impulse);
         }
         
         oscillatePos = Vector3(transform.position.x, transform.position.y, transform.position.z);
         transform.position.y = oscillatePos.y + Mathf.Sin(Time.time * 1.5) * oscillateDist;
         transform.position.x = oscillatePos.x + Mathf.Sin(Time.time * 1) * oscillateDist;
         
     }
Comment
Add comment · Show 1
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 simone84 · May 29, 2011 at 02:27 AM 0
Share

No-one has any ideas?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Anxo · May 29, 2011 at 08:34 PM

Not sure if it would help but you should always have Rigidbody activity like AddForce in a FixedUpdate instead of an update, the forece might be applied multiple times and does wired things in Update.

on a none related note, is there a reason why you use CurrentPos = Vector3(transform.position.x, transform.position.y, transform.position.z); instead of CurrentPos = transform.position? I am still learning a lot and I wonder if there is a benefit to it because I thought with checking for x y z independently would force the code to look for the game object's transform 3 times.

Comment
Add comment · Show 2 · 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 simone84 · May 30, 2011 at 02:52 AM 0
Share

The mouse click has to be in update to always be detected, and because the addforce only occurs on button up, not every frame, it seems ok.

The currentPos is probably just leftover from when I was assigning the x and y separately (without z). I should change it.

Thanks for the suggestions.

avatar image Anxo · May 31, 2011 at 04:10 PM 0
Share

did you find a solution? maybe try a Debug.Log just in front of the AddForce to see when the problem happens if it is called more than one time on the problem click.

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

2 People are following this question.

avatar image avatar image

Related Questions

Rigidybody2D addforce at constant speed 1 Answer

Not understanding how rigidbody2D.AddForce works 0 Answers

A flaw in my 2d chase or flee script 1 Answer

Player launches into air when hitting steep slopes 0 Answers

Declaring myRigidbody 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