- Home /
I need help on my parented Collider2D and Vector 3 Script
Hey guys.
I'm making a 2D Platformer game. Right now I'm working with the enemy script. On my enemy there is a collider2D parented with the enemy. When the player get in contact with the parented collider the stomp bool goes to true. The problem with that is, if the player stays idle and the enemy get contact with the player then the enemy dies and the current health from the player falls down. So now I want to make it so when the player is not grounded(that means he is jumping right now) and get contacted with the collider, then the stomp goes to true not otherwise. In my PlayerControllerScript is a variable "bool grounded = false;" with a groundcheck. How can I implement this into my stomp.cs script?
Image from the enemy:
Here is the stomp script:
using UnityEngine;
using System.Collections;
public class stomp : MonoBehaviour {
public string tag = "Player";
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == tag)
{
transform.root.gameObject.GetComponent<SendDamageCollider>().stomp = true;
}
}
}
My second problem..
When the stomp gets true in the SendDamageCollider script everything works great except "if(fall)". The problem there is, that the x axis from the enemy goes to 0 when the enemy is falling. I want it that the x axis stays same as the enemy. In the "if(stomp)" the x axis is the same as the enemy. I don't know why. The SendDamageCollider script is residing with the enemy.
Code snippet from my SendDamageCollider:
public bool stomp;
public bool fall;
void Update ()
{
if(stomp)
{
gameObject.GetComponent<Walker>().walkSpeed = 0.0f;
transform.position = new Vector3 (transform.position.x, transform.position.y, 4);
transform.localScale = new Vector3(2, 1, 2);
fall = true;
//stomp = false;
}
if(fall)
{
transform.position -= new Vector3 (transform.position.x, 0.05f);
}
if (transform.position.y < -16)
{
Destroy (gameObject);
}
}
I hope you guys can help me and also I hope that i could explain it. I'm a newbie at scripting. Sorry for that.
Edit: Here is a video, what i exactly mean. https://www.youtube.com/watch?v=TxCE0hrIRwo
Please use a proper title for you question. "I need help for my scripts" is NOT a proper question and will actually stop people from co$$anonymous$$g to your question and trying to help you.
Answer by Wuzseen · Mar 21, 2014 at 03:00 PM
I'm not sure why you have stomp separate from your playercontroller. Presumably then, you did not make the player controller.
Either way, reference the player controller in your stomp script:
using UnityEngine;
using System.Collections;
public class stomp : MonoBehaviour {
public PlayerController controller;
public string tag = "Player";
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == tag && !controller.grounded)
{
transform.root.gameObject.GetComponent<SendDamageCollider>().stomp = true;
}
}
}
I'm not sure what your classes and such are called so you'll have to fill those in yourself, but don't forget to link the object in the inspector!
However, I wouldn't really suggest doing this if you want a stomp effect.
Why not compare the y value of the center of your player and the enemy? If the player is above, then stomp. If not, don't stomp.
Also, in your screenshots I think i can offer you some helpful tips and good practices that might help you out. You have a lot of similarly named game objects, why not make these all children of an empty game object for organizational purposes? (all of your green_blocks). Setting up a heirarchy is very helpful instead of having everything be on the top level. You can then prefab that whole "tree" of blocks and reuse it in different places.
I would also look up coroutines, messaging, and events but that's a big topic best left for a different question...
Your second problem is easy though!
if(fall)
{
transform.position -= new Vector3 (0, 0.05f);
}
You're doing a delta (-=) operation, if you subtract the x position of the current object from its position, it will wind up at 0. So the new Vector3 should have 0 in the x parameter.
Because the stomp is on the second collider, which is parented with the enemy.
Yeah it's work perfectly. Thanks so much.
That is a good idea with the y value. The problem is I'm a newbie at scripting and I can't do it without any helps. :/ Let's see how I can make it.
Don't irritate you from the screenshots. I have a leveldesigner and they create a lot of game objects. I know I must to do some changes. But anyway thanks for the tips.
Thanks, the second problem is also done.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140746im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
smoothly change position of an Object 1 Answer
Get closest Vector3 position from a GameObject and two Transforms (and the line inbetween them) 2 Answers
Help with this script? Keeps moving my objects around in run-time? 0 Answers
Unity2d rotation, transform.position and raycasting question 0 Answers