- Home /
Problem Trying to Apply Non-Kinematic Velocity to Rigidbody
Hello everyone, I've been attempting to apply the velocity (or momentum) from non-kinematic moving objects to a rigidbody player object. As for why, its a long story but to just get to the point: I can't use physics to move these platforms, so I need to apply the movement of the platforms onto the rigidbody player instead (so that when they leave the platforms, the momentum of the platform is carried by the player).
A bit of info on these platforms: these platforms can (and in my testing case will:) move up and down and sideways (simultaneously), pausing at their highest and lowest points.
Now, I've got a script that sort of works (when the platforms are stationary everything works fine, and when moving the player seems to correctly inherit the X and Z velocity while moving up or down just fine). However, the Y velocity (while moving up, down seems to work just fine) is a mess. Either the player jumps FAR too strongly, or jumps an appropriate height, but then on landing back on the platform will shoot up into the air again (and this time much higher).
I've also noticed that on the logs, the "velocity" of both X and Y change dramatically, sometimes doing things like (velocity = 1, 9, 1, 9 , 51, 3, 9, etc). I don't see any reason why this would be the case (for such high variance).
If you have any idea why this is happening I would love to know, this problem is really driving me crazy.
Here is the cleaned up script (and just so you know, the player jump is handled through rigidbody.addforce with Impulse as the force method.
using UnityEngine;
using System.Collections;
public class VerticalColumnMovement : MonoBehaviour {
public Vector3 startingPosition;
public Vector3 endingPosition;
public float verticalMovementDistance;
public float horizontalMovementDistance;
public float movementSpeed;
public float rectractTimer;
public float restartTimer;
public bool nextPhase;
public bool inMotion;
//public GameObject colliderToIgnore;
public Vector3 directionVector;
public PlayerMovement playerMovement;
public Vector3 previousPosition;
public float velocityX;
public float velocityY;
public Vector3 currentPosition;
void Awake ()
{
nextPhase = false;
//Debug.Log (nextPhase);
inMotion = false;
//Debug.Log (inMotion);
startingPosition = transform.position;
endingPosition = new Vector3 (transform.position.x + horizontalMovementDistance, transform.position.y + verticalMovementDistance, transform.position.z);
playerMovement = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMovement>();
}
void Update ()
{
velocityX = ((currentPosition.x - previousPosition.x)) / Time.deltaTime;
velocityY = ((currentPosition.y - previousPosition.y)) / Time.deltaTime;
previousPosition = transform.position;
MovePosition ();
currentPosition = transform.position;
}
void OnTriggerEnter (Collider other)
{
if (other.tag == "Player")
{
other.transform.parent = transform;
}
}
void OnTriggerExit (Collider other)
{
if (other.tag == "Player")
{
Vector3 playerCombinedVelocity = new Vector3 (velocityX + other.rigidbody.velocity.x, velocityY + other.rigidbody.velocity.y, rigidbody.velocity.z + other.rigidbody.velocity.z);
//Debug.Log (velocityY);
other.rigidbody.velocity = playerCombinedVelocity; // or +=??
other.transform.parent = null;
}
}
void MovePosition ()
{
if (nextPhase == false && inMotion == false)
{
transform.position = Vector3.MoveTowards (transform.position, endingPosition, movementSpeed * Time.deltaTime);
//Debug.Log (Vector3.Distance(endingPosition, rigidbody.position));
if (Vector3.Distance(transform.position, endingPosition) < 1.01f)
{
//Debug.Log ("distance check achieved, going down");
inMotion = true;
if (inMotion == true)
{
StartCoroutine (WaitForRetract(rectractTimer));
}
}
}
if (nextPhase == true && inMotion == false)
{
transform.position = Vector3.MoveTowards (transform.position, startingPosition, movementSpeed * Time.deltaTime);
//Debug.Log (Vector3.Distance(startingPosition, rigidbody.position));
if (Vector3.Distance(transform.position, startingPosition) < 1.01f)
{
//Debug.Log ("distance check achieved, going up");
inMotion = true;
if (inMotion == true)
{
StartCoroutine (WaitForRestart(restartTimer));
}
}
}
}
IEnumerator WaitForRetract (float time)
{
yield return new WaitForSeconds (time);
//Debug.Log ("I finished waiting");
nextPhase = true;
//Debug.Log ("next pahse is " + nextPhase + " called from 1st block");
inMotion = false;
//Debug.Log ("in motion is " + inMotion + " called from 1st block");
}
IEnumerator WaitForRestart (float time)
{
yield return new WaitForSeconds (time);
//Debug.Log ("I finished waiting");
nextPhase = false;
//Debug.Log ("next pahse is " + nextPhase + " called from 2nd block");
inMotion = false;
//Debug.Log ("in motion is " + inMotion + " called from 2nd block");
}
}
Your answer
Follow this Question
Related Questions
Force to Velocity scaling? 2 Answers
RigidBody clips into corners 3 Answers
Rigidbody character controller can't walk on stairs 0 Answers
RigidBody Script Conflict 1 Answer
Moving a player with Rigidbody 2 Answers