Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 unity_17tvermeir · Aug 29, 2021 at 12:49 AM · 2d2d-platformerknockback

How can I add knockback to a prefab?

I am making a 2d platformer game for a hs project, I currently have a player game object that experiences knockback with enemies perfectly, but when he dies, the prefab for some reason doesn't experience knockback, even when it is the exact copy of the original game object. I have a hunch that it is something to do with this line var player = GetComponent();. This is the current script for my knockback. The variable is my player controller script

  private void OnCollisionEnter2D(Collision2D other)
     {
         if (currentHealth > 0)
         {
             var player = GetComponent<PlayerMovement>();
             if (other.gameObject.tag == "Enemy")
             {
                 TakeDamage(20);
                 
                 player.knockbackCount = player.knockbackLength;
                 if (player.facingRight)
                 {
                     player.knockFromRight = true;
 
                 }
                 else if(!player.facingRight)
                 {
                     player.knockFromRight = false;
                 }
             }
 
 
 
   

and this is my code for respawning the prefab

 public void Respawn() {
         GameObject Player = Instantiate(playerPrefab, spawnPosition.position, Quaternion.identity);
         
         cam.Follow = Player.transform;

 

And then this is the script inside my player controller

 if (canMove && knockbackCount <= 0)
         {
             rb.velocity = new Vector2(movementSpeed * mx, rb.velocity.y);
         }
         else
         {
             if (knockFromRight)
             {
                 rb.velocity = new Vector2(-knockback, 0.0f);
             }
             if(!knockFromRight)
             {
                 rb.velocity = new Vector2(knockback, 0.0f);
             }
             knockbackCount -= Time.deltaTime;

Any help would be appreciated and let me know if you need more explained.

Comment
Add comment
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

1 Reply

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

Answer by GeroNL · Aug 29, 2021 at 07:19 AM

Hello, if you use the prefab and it has reference you need to find in Start or in Awake, like this:

 // example in enemy code
 // instans    
 Player player;    

 private Awake()
 {
     player = GameObject.Find("NameGameObject that have Player)").GetComponent<Player>();
 }

Maybe it not work cause your some or your prefab not reference some object.


Hope it help.

Comment
Add comment · Show 4 · 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 unity_17tvermeir · Aug 29, 2021 at 08:22 AM 0
Share

@GeroNL I don't exactly understand what you are trying to say sorry, I am quite new to this whole thing. I already have an awake function that is used to create an instance to help respawn my player that is attached to a different script. Are you suggesting I try the code above in that function or in the one where my player takes knockback? private void Awake() { instance = this; }

avatar image GeroNL unity_17tvermeir · Aug 29, 2021 at 08:53 AM 0
Share

Your enemy code has a player? if it has, are you instant or set it on unity inspector? if it like that, the enemy in scene doing well, but not for prefab. prefab can not be set with object out of it(the prefab, but it can set the objects in its child). to set the player you need to find it with code that i already suggest, put it on Start or Awake, and if you already have that(Awake or Start) just put on it on the first line, cause if you use it (like : player.something) you need to set it first.

avatar image unity_17tvermeir GeroNL · Aug 29, 2021 at 09:57 AM 0
Share

@GeroNL Thank you so much, it worked. Absolute legend and I hope you have a good day wherever you are!

Show more comments

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

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

Related Questions

Knockback effect in 2D. 1 Answer

Platforms are been generated more and more to the left or right 0 Answers

Help Me! I want to make a pixel fairy! 1 Answer

Best 2d User Interface for Mobile and Other platforms 1 Answer

How to make layers become visible with a button prompt?,How to make entire layers active/inactive with a button press 0 Answers


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