- Home /
Im having trouble making my character throw a gameobject
I am currently stuck trying to make my character throw a helmet, what i am trying to do is make the helmet GameObject invisible, then when they get to a certain point make it visible, this GameObject is a child of a bone in the characters arm, this way the helmet will go along with the animation. Then when its visible and the player presses 'F' it will spawn a helmet GameObject on that GameObject, hide the old one and give it force in the direction that i want. the problem is when i try to hide the GameObject it either doesnt hide at all or it will hide the entire thing, so the character as well.
Below is the code i am trying to use, the commented out parts are what i have tried and have left just in case i might need it.
It would be a huge help if you could show me whats wrong with my code or tell me what i need to do.
Thank you, Michael
void Start () {
gameObject.GetComponent<Renderer>().enabled = false;
spawnHelmetPosition = new Vector3 (stillHelmetHolderTRFM.transform.position.x, stillHelmetHolderTRFM.transform.position.y, stillHelmetHolderTRFM.transform.position.z);
helmetHolder = GetComponent<Transform>();
movingHelmetTRFM = GetComponent<Transform> ();
movingHelmetRB = GetComponent<Rigidbody> ();
movingHelmetGO = GetComponent<GameObject> ();
}
// Update is called once per frame
void Update () {
if (isCarryingHelmet) {
gameObject.GetComponent<Renderer>().enabled = true;
// gameObject.renderer.enabled = true;
// helmetTRFM.transform.localRotation = transform.rotation;
// helmetTRFM.transform.position = helmetHolder.position;
canThrowHelmet = true;
}
if (Input.GetKeyDown("f") && (canThrowHelmet))
{
Instantiate (movingHelmetPRFB, spawnHelmetPosition, Quaternion.Euler(-31.475f, 58.482f, -176.822f), helmetHolder);
isCarryingHelmet = false;
}
if (!isCarryingHelmet) {
gameObject.GetComponent<Renderer>().enabled = false;
canThrowHelmet = false;
}
}
if (Input.Get$$anonymous$$eyDown("f") && (canThrowHelmet)) { Instantiate (movingHelmetPRFB, spawnHelmetPosition, Quaternion.Euler(-31.475f, 58.482f, -176.822f), helmetHolder);
isCarryingHelmet = false;//here youre making it false when its thrown
}
//after throwing if (!isCarryingHelmet) { //here youre making it hide when its thrown ,Here is the fault i think gameObject.GetComponent().enabled = false; canThrowHelmet = false; }
Answer by Mikeburdge · Jan 18, 2018 at 12:13 PM
@raoul_95, im sorry i should have been more specific, i think that you are picking up on is what i described a little bit in the description i have. There are two instances of the helmet, one as a placeholder for looks and the other is to be spawned on it when the character presses the 'F' key. when the character is holding the helmet it will become visible, then when its visible and he presses the 'F' key it will spawn the new helmet on the coordinates of that placeholder helmet, make the placeholder invisible then give the new helmet that i spawned force to go forward.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Can I use AddComponent to add (this.script) to an object? 1 Answer
Prefab not loading in data from Inspector 1 Answer
[Solved] How to get non repeating random positions? 5 Answers