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 SVGK · Jul 03, 2014 at 09:39 PM · rigidbodyvelocityplatform

Velocity calculation for non-rigidbodies.

Hello, I've got a platform movement script in place for a game, however, I need a calculation of the platform velocity, however, the movement doesn't use velocities or forces, or any rigidbody component, so I'm mostly on my own, however, the previous questions about this subject don't seem to be working:

 using UnityEngine;
 using System.Collections;
 
 public class PlatformMover : MonoBehaviour 
 {
     public float speed;
 
     public Vector3 velocity;
     public Vector3 destiny;
     public Vector3 origin;
     public Vector3 lastPosition;
 
     public bool reached = false;
 
     void Update()
     {
         velocity.x = ((transform.position - lastPosition).magnitude /Time.deltaTime);
         velocity.z = ((transform.position - lastPosition).magnitude /Time.deltaTime);
         lastPosition = transform.position;
     }
     
     void FixedUpdate()
     {
         // For these 2 if statements, it's checking the position of the platform.
         // If it's at the destination spot, it sets the Switch to true.
         if(transform.position == destiny || Vector3.Distance (transform.position, destiny) < 0.3)
         {
             reached = true;
         }
         if(transform.position == origin || Vector3.Distance (transform.position, origin) < 0.3)
         {
             reached = false;
         }
         
         // If Switch becomes true, it tells the platform to move to its Origin.
         if(reached)
         {
             rigidbody.velocity = (origin - transform.position).normalized * speed;
         }
         else
         {
             // If Switch is false, it tells the platform to move to the destination.
             rigidbody.velocity = (destiny - transform.position).normalized * speed;
         }
     }

The code for calculating velocity, well, it doesn't work, the velocity value fluctuates wildly between 0 and several higher values, at a very high framerate, this means it's inaccurate all the time, putting it in FixedUpdate keeps it at a constant value of 1, regardless of the direction the platform is moving in, or the speed.

What exactly am I doing wrong here?, and how do I deal with it?, I need an accurate representation of the velocity so I can keep rigidbody player characters glued to the platform when they stand on it.

Comment
Add comment · Show 4
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 Kiwasi · Jul 03, 2014 at 11:21 PM 0
Share

Why not just use speed? You are moving the transform directly, by definition you know its velocity.

avatar image Kiwasi · Jul 03, 2014 at 11:36 PM 0
Share

Couple of things to note. Someone with more time then me may be able to hash this out into an actual answer.

Velocity is a vector, not a scalar. Hence you don't want to use .magnitude, use the raw Vector3 ins$$anonymous$$d. You will want to get a Vector3 as your result in order to have any meaningful effect on your player.

Put the calculation code in the same Update/FixedUpdate cycle as your movement code. This will ensure you are getting the data immediately after the transform.position has been updated.

If you have no rigidbody to reference for velocity then what are lines 38 and 43 doing?

avatar image SVGK · Jul 04, 2014 at 04:31 PM 0
Share

Oh damn, Bored$$anonymous$$ormon, I'm such an idiot, I've rewritten this piece of code like three times already and haven't seen it in a few weeks, I didn't notice that I had written it to use velocity.

Damn, the question is pretty much useless now, should I delete it or what?, taking the raw Vector3 gives me an error about conversion of floats to Vector3s.

avatar image Juice-Tin · Jul 04, 2014 at 07:55 PM 0
Share

If Bored$$anonymous$$ormon helped you, he should put his comment in the form of an answer so you can mark it as answered. :)

Edit: If you want the speed of a Vector3 in float, you can use velocity.magnitude. Or you can get a vector with something like Vector3.right*speed, if you want it to move to the right at a certain speed.

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

23 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

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Player slips off moving platform if Animator Component enabled 0 Answers

Getting the player to move with platform: Velocity, DistanceJoint, or...? 2 Answers

Limiting the top speed of a rigidbody in 3d space? 1 Answer

how to limit horizontal input when jumping 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