- Home /
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?
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 ?
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 );
}
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.
I will try both methods and see which one works best. Thank you both very much for responding.
Your answer
Follow this Question
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