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 Greyeeh · Aug 20, 2015 at 11:44 PM · c#rigidbodycharactercontroller

Push player away from the enemy

I've made my own controller for the game I'm trying to make but I've ran into some issues with it. Whenever the enemy is approaching me and I don't stop my character in time, my character teleports on top of the enemy's head.

Push away:

 void OnControllerColliderHit(ControllerColliderHit hit)
 {
     if (hit.gameObject.GetComponent<EnemyController>())
     {
         Debug.Log("!");
         float pushPower = 0.05f;
         Vector3 pushDir = hit.transform.forward;
         characterController.Move(pushDir * pushPower);
     }
 }

Movement:

 void FixedUpdate()
 {
     RaycastHit hitInfo;
     Physics.SphereCast(transform.position, characterController.radius, Vector3.down, out hitInfo, characterController.height / 2f);
     desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
 
     desiredMove = (transform.forward * Input.GetAxis("Vertical") + transform.right * Input.GetAxis("Horizontal")).normalized;
 
     if(characterController.isGrounded)
     {
         moveDir.x = desiredMove.x * speed;
         moveDir.z = desiredMove.z * speed;
         isJumping = false;
 
         if(Input.GetButtonDown("Jump"))
         {
             moveDir.y = jumpPower;
             isWalking = false;
             isJumping = true;
             canRun = false;
         }
 
         if(Input.GetButton("Sprint") && canRun)
         {
             isWalking = false;
             isRunning = true;
             moveDir.x = desiredMove.x * runSpeed;
             moveDir.z = desiredMove.z * runSpeed;
         }
         else
         {
             isRunning = false;
         }
 
        
     }
     else
     {
         Falling();
     }
     characterController.Move(moveDir * Time.fixedDeltaTime);
 }

I've decided to add a function to push player away if he's too close to the enemy but I can't really get it to work right. Right now it pushes player in the forward direction of the enemy so it works fine when you're approaching the enemy from the front, in other cases my player just teleports in front of the enemy. I wanted to push the player away to the direction he came from but I can't really think of any way to do it. Could anyone help me out with it?

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 Firedan1176 · Aug 21, 2015 at 01:48 AM 0
Share

You could try using Rigidbody.AddForce with Force$$anonymous$$ode.Impulse with the difference between your player and the enemy's position, something like this:

 GetComponent<Rigidbody>().AddForce((transform.position - enemy.transform.position).normalized * pushAmount, Force$$anonymous$$ode.Impulse);

Now I just typed this in the comment, so there are a few things that could go wrong.. You may need to reverse the transform.position and the enemy.transform.position, just switch them around if it doesn't push you backward.

It's untested, but that could work.

Another idea is you could just simply only move your character as long as there is no collisions with an enemy. You could set your enemy's tag to a new tag called "Enemy", and do something like this:

 bool isColliding;
 
 void OnCollisionEnter(Collision col) {
 
     if(Collision.CompareTag("Enemy"))
         isColliding = true;
 
 }
 
 void OnCollisionExit(Collision col) {
 
     if(Collision.CompareTag("Enemy"))
         isColliding = false;
 
 }

Now again, I probably messed it up. You may need to replace collision with collider, and make sure You OR the enemy have a rigidbody.

Good luck.

1 Reply

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

Answer by Greyeeh · Aug 21, 2015 at 07:02 PM

Here's the final code:

 void OnControllerColliderHit(ControllerColliderHit hit)
  {
      if (hit.gameObject.GetComponent<EnemyController>())
      {
          float pushPower = 0.05f;
          Vector3 pushDir = transform.position - hit.transform.position;
          characterController.Move(pushDir * pushPower);
      }
  }
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

27 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

Related Questions

Moving a CharacterController Object together with a RigidBody Object 1 Answer

Distribute terrain in zones 3 Answers

Convert Rigidbody code to CharacterController usable 0 Answers

Collision on character controller (fps player) 0 Answers

Having problems with animation and character controller velocity not matching 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