Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Creative Inventory · Oct 24, 2015 at 10:24 AM · playerobjectbounce

pushback player on collision

Okay I have a 2D topdown game where my player moves anywhere by mouse click. I want my enemies to push back my player on collision. I have a movement script on my enemies as well. Maybe the problem is that i need to disable the movement script on my player once it collides with the enemy so that my player would get pushed back then re-enable it after it get pushed back. But how would I do that? I need a script that bounce my player of my enemy on collision! I've looked on Google but I couldn't find any that match my description. Could anybody help me please! Thank you! here's my player movement script:

   using UnityEngine;
   using System.Collections;
   using UnityEngine.UI;

   public class MainPlayer : MonoBehaviour {

 public float speed = 5f; 
 public Text ScoreText;
 public AudioClip Coinsound;

 private Vector3 target;
 private int Score;
 
 void Start () 
 {
     target = transform.position;
     Score = 0;
     SetScoreText ();
 }
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag ("Pick Up")) 
     {
         other.gameObject.SetActive (false);
         Score = Score + 1;
         SetScoreText ();
         AudioSource.PlayClipAtPoint (Coinsound, transform.position);
     }
 }
 
 void SetScoreText ()
 {
     ScoreText.text = "Score: " + Score.ToString ();
 }
 void Update () {
     if (Input.GetMouseButtonDown (0)) {
         target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         target.z = transform.position.z;
     }
     transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
 }
 void LateUpdate () {
     var left = Camera.main.ViewportToWorldPoint (Vector3.zero).x;
     var right = Camera.main.ViewportToWorldPoint (Vector3.one).x;
     var top = Camera.main.ViewportToWorldPoint (Vector3.zero).y;
     var bottom = Camera.main.ViewportToWorldPoint (Vector3.one).y;
     float x = transform.position.x, y = transform.position.y;
     if (transform.position.x <= left + GetComponent<Renderer> ().bounds.extents.x) {
         x = left + GetComponent<Renderer> ().bounds.extents.x;
     } else if (transform.position.x >= right - GetComponent<Renderer> ().bounds.extents.x) {
         x = right - GetComponent<Renderer> ().bounds.extents.x;
     }
     if (transform.position.y <= top + GetComponent<Renderer> ().bounds.extents.y) {
         y = top + GetComponent<Renderer> ().bounds.extents.y;
     } else if (transform.position.y >= bottom - GetComponent<Renderer> ().bounds.extents.y) {
         y = bottom - GetComponent<Renderer> ().bounds.extents.y;
     }
     transform.position = new Vector3 (x, y, transform.position.z);
 }

}

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
0

Answer by Statement · Oct 24, 2015 at 10:39 AM

Maybe the problem is that i need to disable the movement script on my player once it collides with the enemy so that my player would get pushed back then re-enable

Yes.

 if (canMove)
     transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);

I don't know how you want to do the pushback though.

 void OnTriggerEnter2D(Collider2D other)
  {
     if (canMove && other.CompareTag ("Hurt")) 
     {
         StartCoroutine(Knockback());
     }
 }

 IEnumerator Knockback() {
     canMove = false;
     // coroutine implementation for knocking back the player etc... 
     // maybe play animation and wait for animation to finish?
     canMove = true;
     yield break;
 }
Comment
Add comment · Show 5 · 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 Creative Inventory · Oct 25, 2015 at 08:28 AM 0
Share

Thank you for replaying back, i'll give it a try!!!

avatar image Creative Inventory · Oct 25, 2015 at 08:31 AM 0
Share

Um, I've tried it out, but an error comes up saying "The name `can$$anonymous$$ove' does not exist in the current context" Then what should i change it to? Thank you!! @Statement

avatar image Statement · Oct 25, 2015 at 10:09 AM 0
Share

@Creative Inventory You need to make a variable for can$$anonymous$$ove.

 bool can$$anonymous$$ove = true;

Right up the top of your class where your other variables are like target and score.

avatar image Creative Inventory Statement · Oct 25, 2015 at 12:51 PM 0
Share

I've tried it out, it works perfectly, but I don't think it's what I'm looking for. What I want is that once my player collides with an enemy, my player should bounce back, kinda like when sonic (2D) bounce back when he come in contact with this:

![alt text][1] [1]: /storage/temp/56906-sonic-bounce.jpg

I want my player to do the same whilst my enemies moves as well.
Sorry if I didn't mention it before!!! Thank you anyway!! @Statement

sonic-bounce.jpg (4.9 kB)
avatar image Statement Creative Inventory · Oct 25, 2015 at 01:42 PM 0
Share

I don't see the problem. I dont know how that button interacts with sonic. But anyway, if you hit a monster, play an animation to bounce you back.

Your enemies can move as well. Just make sure they have a rigidbody (possibly $$anonymous$$inematic if you are not using physics responses) and a collider (possibly a trigger).

Once you detected you hit an enemy, do something. Play an animation. $$anonymous$$ake a puff of smoke. Enable physics and make a ragdoll. Anything goes.

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

33 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

Related Questions

Death from collision 1 Answer

Is there a way to make an Object pass over a Text? 0 Answers

What is the best way to script a trigger that moves a object from point a to point b in C# (unity 5) 1 Answer

How to put an object in front of the player when clicked 0 Answers

I'm trying to create an object I can pick up and move in a 2D platformer. 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