Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How would I move to a point with Rigidbody?

Discussion in 'Scripting' started by Marscaleb, Aug 1, 2014.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,036
    I've created some scripts that move an object to a specific point using transform.position = Vector3.Lerp(transform.position, endPosition, distanceCounter); which turned out to be very reliable because it totally ignored all other physics.

    But I'm making a similar script that needs to have it's velocity be something that can be read by other scripts. So, I need to actually have a velocity. So, I'd need to move the object using rigidbody instead of directly modifying the position.

    But I honestly have no idea how to do that. Sure I can apply a velocity easily enough. but I'm trying to make the velocity move the object to a specific point in space. (And according to a speed I will provide.) I'd need to be able to compute a vector toward the point in space, and then convert that into a velocity. I honestly don't know how to do that.
    (Unless there's already some existing function that moves a rigidbody to a specific point.)
     
  2. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    You could simply calculate the distance to be travalled in a certain time frame, and make it x/1 so it fits with the m/s that untity uses, and use that as it is the velocity of the object.

    If the obove is wrong (which it well may be), there is a major flaw in trying to get to a specific point with a rigidbody; the physics are applied, so unless you make your friction infinity the object will always slide a bit following the force, and so it's hard to get an object to be exactly where it should be
     
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,036
    It doesn't need to hit the exact point; I can just check to see if it gets within some very minute distance of the point.

    I don't see how the distance to be traveled would be relevant, unless I needed to find an unknown speed to arrive at a specific time. I need a vector for direction which is then multiplied by a speed. But how do I get the direction to an arbitrary point from another arbitrary point?
     
  4. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    The distance to be travelled multiplied divided by the time period that it was recorded at is your velocity. in your original question you said that you needed the velocity of that script to be accessible by other scripts, so assign the velocity to a public float and find go the normal root of getComponent<YourScript>().Velocity to have access to it.

    I may be understanding this wrong, if so please excuse it