- Home /
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.
Why not just use speed? You are moving the transform directly, by definition you know its velocity.
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?
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.
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.