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 safak93 · Mar 20, 2014 at 09:40 PM · vector3getcomponenttransform.position

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: alt text

alt text

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

squishybox.png (93.6 kB)
enemy.png (144.6 kB)
Comment
Add comment · Show 1
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 Benproductions1 · Mar 21, 2014 at 04:13 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

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 safak93 · Mar 21, 2014 at 04:44 PM 0
Share

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

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

22 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 avatar image

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

Simple script to move object doesn't work 1 Answer


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