- Home /
How to make a gameObject 'Carry' another?
Hello, possible saviour!
My game is a top down shooter. So, in my game enemies try to steal things from you by picking them up. Once they pick them up, I want them to be visibly carrying them. Example :
Mario is always holding up the shell in the same position relative to him no matter what the rotation or position is.
The thing is that I can't get it to work! I've tried some things but they dont work. I want the pick up to work just like the shell mario is picking up.
Using mario as a scenario, I can't get the shell to pop from one location to mario's hands.
-------This script is attached to the enemy character.------
transform.Translate (new Vector3(0,0,speed * Time.deltaTime)); //Makes enemy move
//The code below does not work;
objectBeingCarried.transform.Translate(transform.position.x * Time.deltaTime,
transform.position.y * Time.deltaTime,
speed * Time.deltaTime);
So the question is: How can I make an object appear in another's 'hands' and stay there relative to where its hands are? Should I use parenting to achieve this?
Answer by Aridez · Jul 13, 2015 at 12:58 AM
Maybe you could put the object you want to carry inside the "hand object" so it always keeps its position relative to one hand, you could also do a script that each frame updates the relative position to the hand, something like:
public Transform handtransform = (hand transform);
private vector3 relativeposition = new vector3(relative position you want to set);
void Update() {
this.transform.position = handtransform.position +relativeposition;
}
edit: I forgor to mention, to put the game object inside another via script (if you use the first solution i mentioned) you can do it using gameObject.transform.parent = handtransform;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Mad Level Manager Unlock Level 0 Answers
Offset transform.parent.position - Help 1 Answer
Accessing children of instances vs children of original prefab 1 Answer
getting object local direction? 1 Answer