Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 esitoinatteso · Oct 21, 2014 at 08:45 PM · collisionaddforcefixedupdatecache

Mistake in Function with Collision and AddForce

Hi people! I'm trying to make this function work and I've spotted an error I can't seem to understand.

What this is supposed to do is to make the head of a rope ( which the player can instantiate) act like a Grappling Hook. The hook itself is labelled with a tag that other objects in my game look for when colliding with something. If the tag matches then a SpringJoint is added to the object and the rope is forced to lock at the first contact point of their collision.

This position's floats are stored in the collFloat variables.

Then, if a button is pressed this function is called, but right now it's not working as expected. The first time a rope locks to a target position everything seems fine. From then on things become odd, to say the least:

  1. First of all the cached position is not changing properly, for some reason it returns the correct value in one frame, then swaps back to the original one just before applying the force

  2. The force is multiplying by itself every time a new position is cached... I really don't know why

So, right now it's working just once. Which is very strange. Please, tell me you've got an idea about why it's doing this!

Here's the code I've wrote so far:

         //SO... let's make this very basic
         //I'll call this from a KeyInput If and you'll apply force to me so that I can reach the head of the hook.
         //But where's the head of the hook?
         
         //Here it is... ideally.
         Vector3 hookPosition = new Vector3(collFloatX,collFloatY,collFloatZ);
         Debug.Log("collision X = "+collFloatX); <----- THIS IS THE CALL THAT PRINTS DIFFERENT VALUES
         
         //Then I need the direction and distance toward which I'll apply the force ( I suspect it's gonna apply it for 1 frame)
         //Vector3 distance = hookPosition-thePlayer.transform.position;
         float distance = Vector3.Distance(thePlayer.transform.position,hookPosition);
 
         Vector3 direction = hookPosition-thePlayer.transform.position;
         
         //It IS for just a dozen of frames, but that's good right now because it's enough to start moving
         if(distance > 0.5f){
             thePlayer.rigidbody.AddForce(direction*10000f);//imagine this as a straight line
             //thePlayer.rigidbody.AddForce(Vector3.up*5000f);//Add this and you have an arc.
         }

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

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

27 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

Related Questions

Multiple Cars not working 1 Answer

How to call OnTriggerEnter once 5 Answers

Overlap Detection HELP!! 0 Answers

Detecting whether a player is touching the ground 1 Answer

How to make a death-on-collision script for Air Strike Starter Kit? 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