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 Eks-Squared · May 29, 2015 at 12:42 AM · collisionrigidbodyvelocityinherit

How do I have my player ball inherit the velocity of another gameObject?

 using UnityEngine;
 using System.Collections;
 
 public class PlayerParenter : MonoBehaviour {
 
     void OnCollisionEnter (Collision col) {
 
         if (col.gameObject.tag == "Player")
         {
             col.gameObject.transform.SetParent( gameObject.transform );
         }
 
     }
 
     void OnCollisionExit (Collision other) {
 
         if (other.gameObject.tag == "Player")
         {
             gameObject.transform.DetachChildren();
         }
 
     }
 
 }

When the script above is attached to any gameObject, if the sphere player rigidbody (which I will call spRB) collides with said gameObject, the spRB will parent to that object. My issue happens when the spRB exits collision with the gameObject. The spRB's velocity completely resets upon collision exit. Meaning that even if the gameObject is moving, all spRB velocity will stop when collision exit happens.

I want the spRB to INHERIT the velocity from the gameObject upon collision exit. How can I achieve this effect?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by needforbleed · May 29, 2015 at 09:17 AM

Just create a variable for the velocity and let the player class set its value.

Not tried but it should work something like this:

public class PlayerParenter : MonoBehaviour {

public Rigidbody rig; public static Vector3 yourVariableName{get;set;}//this makes the variable easy to acces for the other class

void Start() { rig=GetComponent();//before you can acces the velocity,you must habe a reference to its rigidboy,this is how it works

}

  void OnCollisionEnter (Collision col) {
 
      if (col.gameObject.tag == "Player")
      {
          col.gameObject.transform.SetParent( gameObject.transform );
      }
 
  }
 
  void OnCollisionExit (Collision other) {
 
      if (other.gameObject.tag == "Player")
      {
          gameObject.transform.DetachChildren();

rig.velocity=YourVariableName;//use the value stored in YourVariableName only ,if the object is detached.

      }
 
  }
 

}

Player class { Rigidbody rig;

void Start() {

rig=GetComponent();//same in this class

}

void Update() {

PlayerParenter.YourVariableName=rig.velocity;//this changes the value of .YourVariableNam every update.

//some stuff

}

} (im not completely sure if its type is Vector3 but just give it a try) But in your code you have never added velocity to your object,can you please post your Player Code too ?

Comment
Add comment · Show 1 · Share
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 Eks-Squared · May 31, 2015 at 04:55 AM 0
Share

The Player Code has a LOT of stuff in it, but I will only get the parts that apply forces to the Player.

 void FixedUpdate () {
 
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3( h, 0, v );
 
         playerRB.AddForce( movement.normalized * speed * Time.deltaTime, Force$$anonymous$$ode.Acceleration );
 
     }
avatar image
0

Answer by Jonnoiscooler · May 29, 2015 at 07:04 AM

Forgive me for any slip-ups in answering, this is my first attempt.

There is a way in Unity to actually find the velocity of a component.

  transform.GetComponent<RigidBody>().velocity

This returns a Vector 3 value which is made up of direction and speed.

With your code, I would use something like this to inherit the velocity, this will also apply the same direction of movement as well.

 transform.GetComponent<RigidBody>().velocity = col.gameObject.transform.getComponent<RigidBody>().velocity
 

Try inserting this under your OnCollisionEnter method. Please let me know if this works or not or if you have anymore questions.

Comment
Add comment · Show 1 · Share
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 Eks-Squared · May 31, 2015 at 04:51 AM 0
Share

I will try both methods and see which one works best. Thank you both very much for responding.

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

21 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

Related Questions

Dragging object out of position 2 Answers

Rigidbody Enemy and Collisions 2 Answers

[SIMPLE] Not loose speed when destroying colliding object 1 Answer

Rigidbody not calling OnCollisionEnter 1 Answer

Collision detections under specific conditions 0 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