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 smokk83 · Jan 20, 2020 at 04:22 PM · rigidbodygravityaddforcespherenewbie

Addforce.forward on a sphere object

Hi pros,

short question. I wanna make a simple beat em up on a small planet. When my player hits an enemy the enemy should fly over the planet. Right now i am moving the enemy after a hit with addforce.

 collision.gameObject.GetComponent<Rigidbody>().AddForce(transform.forward * attackPower);

 

My gravity script takes care that the player is not flying to far away from the planet. So far so good,

but i want that the enemy not simply flys straight forward, i want the enemy to crush on the planet from time to time and only "flys" over the surface and not high in the sky.

I tryed adding

 collision.gameObject.GetComponent<Rigidbody>().AddForce(-transform.up* attackPower); 

but nothing changed. The enemy still not touching the ground and bouncing back.

Any tipps from some experts?

Thx Smokki

Comment
Add comment · Show 2
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 EpicCrownKing · Jan 20, 2020 at 07:01 PM 0
Share

I don't completely understand what you're trying to do here. If you want the enemy to bounce, add a physics material. If you want the enemy to go back to the ground, increase the force of gravity. If this doesn't help, please show your gravity script with a little more explanation of what you are wondering about. Hope this helps.

avatar image smokk83 EpicCrownKing · Jan 21, 2020 at 07:50 AM 0
Share

Hi there,

hmm what i want, i want to hit my enemy with a punch and then he should fly backwards, bounces to the ground, flys even more. Perfect would be when the enemy is spining around hisself when he gets hit, too. So that he not just fly in straight line, even more "realistic".

this is my script for the enemy:

  public GameObject Planet;
   
 
 
     public float speed = 4;
     public float JumpHeight = 1.2f;
 
 
     float gravity = 1000;
     bool OnGround = false;
 
 
     float distanceToGround;
     Vector3 Groundnormal;
 
 
 
     private Rigidbody rb;
 
     public Transform player;
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         rb.freezeRotation = true;
 
         enemyHealth = 100;
         enemy$$anonymous$$axHealth = enemyHealth;
     }
 
    
     // Update is called once per frame
     void Update()
     {
 
         //Local Rotation
 
 
 
 
         $$anonymous$$oveToPlayer();
 
 
         //GroundControl
 
         RaycastHit hit = new RaycastHit();
         if (Physics.Raycast(transform.position, -transform.up, out hit, 10))
         {
 
             distanceToGround = hit.distance;
             Groundnormal = hit.normal;
 
             if (distanceToGround <= 0.2f)
             {
                 OnGround = true;
             }
             else
             {
                 OnGround = false;
             }
 
 
 
         }
 
 
         //GRAVITY and ROTATION
 
         Vector3 gravDirection = (transform.position - Planet.transform.position).normalized;
 
         if (OnGround == false)
         {
             rb.AddForce(gravDirection * -gravity);
 
         }
 
         //
 
         Quaternion toRotation = Quaternion.FromToRotation(transform.up, Groundnormal) * transform.rotation;
         transform.rotation = toRotation;
 
        
 
     }
 
     public bool getHit;
 
     public void $$anonymous$$oveToPlayer()
     {
         transform.position = Vector3.$$anonymous$$oveTowards(transform.position, player.position, 10*Time.deltaTime);
     }
 
     public int enemyHealth;
     public int enemy$$anonymous$$axHealth;


and this my script for the hit :

public class arm : $$anonymous$$onoBehaviour { // Start is called before the first frame update void Start() {

 }

 // Update is called once per frame
 void Update()
 {
    
 }

 public float attackPower;

 private void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.tag == "Enemy")
     {
         collision.gameObject.GetComponent<Rigidbody>().AddForce(transform.forward * attackPower, Force$$anonymous$$ode.Impulse);
       
     }

 }

thx for answers so far :)) you guys are great

1 Reply

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

Answer by lgarczyn · Jan 20, 2020 at 07:13 PM

First, you should use the right ForceMode with AddForce. In this case, for a hit, it should be ForceMode.Impulse. For gravity, it should be ForceMode.Acceleration.


This has a very small effect, but it will save you headache. For example, Acceleration doesn't care about the weight of the object, and will pull everyone the same, just like real gravity. It will also multiply the force by Time.fidexDeltaTime so that changing the framerate of the physics engine doesn't change much about the game.

On the other hand, Impulse will be weaker against a heavier object, but will stay the same regardless of the framerate.


Gravity needs to be applied every single frame to work. By adding gravity only when you punched the enemy, you only made your punch weaker, but he will still fly in a straight line.

Comment
Add comment · Show 3 · 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 smokk83 · Jan 20, 2020 at 07:48 PM 0
Share

Thx for the different force types, that is awesome.

i Already added gravity to my enemys with :

  if (OnGround == false)
         {
             rb.AddForce(gravDirection * -gravity, Force$$anonymous$$ode.Acceleration);
 
         }

now with acceleration. It works better now. With the right power and gravity balance the enemy is slicing over the spehere after a hit.

But the effect, that he gets hit , flys, hit the ground, "bounces" a alittle up and flys again seems like the real difficult for me :D

avatar image lgarczyn smokk83 · Jan 21, 2020 at 10:04 PM 0
Share

I've noticed a few issues, both are that you should basically never touch the transform of a rigidbody (except for scaling).


For your rotation, get and set rigidbody.rotation ins$$anonymous$$d, or better yet: angularVelocity, but that's a bit more complex.


But your problem is likely that when the monster hits the ground, OnGround is automatically true, and you then use transform to move the enemy.


Ins$$anonymous$$d of doing that, use AddForce or set the velocity towards the player, but also add a stun timer that prevents them from moving when they are hit by the player, or when they hit the ground with a high velocity (using OnCollisionEnter and relativeVelocity).


A timer is a pretty simple concept, just have a float called stunTimer. Every frame, decrement it by Time.fixedDeltaTime, and don't take any action if it is above 0. When you are hit, set it to the stun duration.

avatar image lgarczyn smokk83 · Jan 22, 2020 at 10:27 PM 0
Share

Also, to get the enemy spinning, you want AddForceAtPosition

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

170 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

Related Questions

Sphere vs Capsule collider collision resolution 0 Answers

re-orienting velocity 1 Answer

Wrong gravity calculation? 2 Answers

iOS game: Physics with ConstantForce 1 Answer

Erratic behavior of sphere and AddForce? 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