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 boddole · Feb 06, 2014 at 02:59 PM · c#physicsrigidbodyvelocity

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");
     }
 }
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

18 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

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


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