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 /
avatar image
0
Question by guilhermeprata · Sep 13, 2018 at 01:25 AM · 2d gameaddforceknockbackaddrelativeforceaddforce movement

How I can do knockback without OnColisson or OnTrigger

I want my player to have knockback when my enemy hits him but I do not want to use OnTrigger or OnCollision ,I want my player to have knockback when my enemy hits him but I do not want to use OnTrigger or OnCollision

This is script I use, my knockback work but have some bugs the I can't fix

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by I5 · Sep 13, 2018 at 02:04 AM

did you forget to include the script? It's not in your post.

If I understand, you do NOT want to use OnTrigger or OnCollision.

Another option would be use a raycast from the center/core part of whatever enemy collider will be making contact with the player. You'd have to do this in Update, so a raycast every frame and see if and where the raycast hit the player. You'd just raycast from the enemy to the player and if the hit distance was, for example 0.1 (or some distance to indicate they're so close that they've collided). However you won't be able to get fined-grained collision info (like an enemy hand hitting a player's head) unless you use OnColisionEnter.

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

Answer by guilhermeprata · Sep 13, 2018 at 05:38 PM

Só I try with add force but no worked so well this is my script using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class HandleMovement : MonoBehaviour {
 
     Rigidbody2D rb;
     StateManager states;
     HandleAnimations anim;
 
     public float acceleration = 30;
     public float airAcceleration = 15;
     public float maxSpeed = 60;
     public float jumpSpeed = 5;
     public float jumpDuration = 5;
     float actualSpeed;
     bool justJumped;
     bool canVariableJump;
     float jmpTimer;
 
     void Start () 
     {
         rb = GetComponent<Rigidbody2D>();
         states = GetComponent<StateManager>();
         anim = GetComponent<HandleAnimations>();
         rb.freezeRotation = true;
     }
     
     void FixedUpdate () 
     {
         if(!states.dontMove)
         {
             HorizontalMovement();
             Jump();
         }
         if (states.gettingHit && states.onGround && justJumped == false && states.canAttack)
         {
             if (states.lookRight)
             {
                 rb.AddRelativeForce(Vector3.left * 13);
                 states.dontMove = true;
 
             }
             else
             {
                 rb.AddRelativeForce(Vector3.right * 13);
             }
         }
     }
     void HorizontalMovement()
     {
         actualSpeed = this.maxSpeed;
 
         if(states.onGround && !states.currentylAttacking)
         {
             rb.AddForce(new Vector2((states.horizontal * actualSpeed) - rb.velocity.x * this.acceleration,0));
 
         }
         //caso esta sliding
         if(states.horizontal == 0 && states.onGround)
         {
             rb.velocity = new Vector2(0, rb.velocity.y);
         }
     }
     void Jump()
     {
         if(states.vertical >0)
         {
             if(!justJumped)
             {
                 justJumped = true;
 
                 if(states.onGround)
                 {
                     anim.JumpAnim();
 
                     rb.velocity = new Vector3(rb.velocity.x, this.jumpSpeed);
                     jmpTimer = 0;
                     canVariableJump = true;
                 }
             }
             else
             {
                 if(canVariableJump)
                 {
                     jmpTimer += Time.deltaTime;
 
                     if(jmpTimer < this.jumpDuration / 1000)
                     {
                         rb.velocity = new Vector3(rb.velocity.x, this.jumpSpeed);
                     }
                 }
             }
         }
         else
         {
             justJumped = false;
         }
     }
 
     public void AddVelocityOnCharacter(Vector3 direction, float timer)
     {
         StartCoroutine(AddVelocity(timer, direction));
     }
 
     IEnumerator AddVelocity(float timer,Vector3 direction)
     {
         float t = 0;
 
         while(t<timer)
         {
             t += Time.deltaTime;
             rb.AddForce(direction * 2);
             yield return null;
         }
     }
 
 }
 
Comment
Add comment · 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

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

101 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

Related Questions

How am I able to move my (Player) character in the opposite direction of where I am shooting? 3 Answers

Adding force to mouse position adds force relative to screen center 0 Answers

Projectiles always going to the left and not forward 2 Answers

AddForce knockback x axis according to mouse.position more then y axis (2D game) 0 Answers

Rigidbody2d.AddForce behaving odd 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