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 shamunity · Oct 01, 2015 at 12:04 PM · physicsinputcontrolsbots

What's the best way to simulate input for physics-driven, CPU-controlled actors?

I'm building a game in the style of Super Monkey Ball. Both the enemy and the player use the same mesh etc and the only difference is color. Every actor should be driven by AddRelativeforces.

For my player, it's easy - I capture Input.GetAxis and feed this to a move vector, then apply forces proportionally. No problem there.

But I'm at a loss on how to move my CPU-controlled enemies. If i manually apply forces, the move vector does not smoothly increase in magnitude over time like it does with my physical player: this is because Input.GetAxis returns a -1...1 value proportional to the 'strength' of the joystick move (this is simulated by unity when using a keyboard).

I was thinking Lerping to a certain value but I'm not sure how to do it... any help would be appreciated.

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

Answer by Tomer-Barkan · Oct 01, 2015 at 01:37 PM

The correct Object Oriented design in my opinion, would be to have the "Actor" class, that will be used for both player and enemies. This actor class will be attached to the mesh object, and will contain an API to control this actor. In your example, it should have a method called "AddRelativeForces(Vector2 force)", that will apply the relevant forces on the actor's rigidbody:

 public class Actor : MonoBehaviour {
     protected void AddRelativeForces(Vector2 force) {
         GetComponent<RigidBody2D>().AddForce(force);
     }
 }

Then, you will have two different controllers. One for the player, and one for the enemy. They will both make use of the actor's API, but each will make it's own decision of when to apply the force. The player will make the decision according to the keyboard input, and the enemy will make the decision according to your AI algorithm.

 public class PlayerControl : MonoBehaviour {
     public Actor actor;

     void Update() {
         Vector2 force = new Vector2(Input.GetAxis("playerX"), 0);  // set force depending on controls
         force = force * Time.deltaTime;  // multiply by deltaTime to keep force independent of framerate

         actor.AddRelativeForces(force);
     }
 }

And the enemy:

 public class EnemyControl : MonoBehaviour {
     public Actor actor;

     void Update() {
         Vector2 force = new Vector2(GetAiForceDirection(), 0);  // set force depending on AI decision
         force = force * Time.deltaTime;  // multiply by deltaTime to keep force independent of framerate

         actor.AddRelativeForces(force);
     }

     float GetAiForceDirection() {
         // Implement your AI algorithm here
     }
 }
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 shamunity · Oct 01, 2015 at 03:14 PM 0
Share

Thank you, that is useful.

The part I'm struggling with however is being able to send the input as a smooth increase from 0 to 1 at the exact same speed Unity does it when it simulates an analog joystick with the keyboard.

avatar image shamunity shamunity · Oct 01, 2015 at 03:15 PM 0
Share

In other words, in your code when you apply Vector2 as a force you do so immediately, whereby if it moved the joystick in the same direction I would get a gradual increase in applying the force.

avatar image Tomer-Barkan shamunity · Oct 01, 2015 at 04:32 PM 0
Share

Hmm, not sure how unity calculates it's gravity/sensitivty... but if you use a joystick, then it's simply 0..1, linearly, where 0 is when the stick is centered, and 1 when it's all the way to the side (or 0..-1 for the other side).

If you use a button, it might be easier to make the input "snap", which means it will be either -1, 0 or 1. Then you can do your own formula for smoothing, and use the same formula for the AI.

A simple smoothing formula would be:

 Vector2 currentSmoothValue = Vector2.zero;

 void Update() {
     Vector2 playerInputDirection = new Vector2(Input.GetAxis("horizontal"), Input.GetAxis("vertical"));
     currentSmoothValue = Vector2.$$anonymous$$oveTowards(currentSmoothValue, playerInputDirection, Time.deltaTime * smoothSpeed);
     actor.AddRelativeForce(currentSmoothValue * Time.deltaTime);
 }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating custom input actions 1 Answer

Platform dependent control schemes 0 Answers

How can I have good acceleration mechanics for a 2d spaceship? (C#) 1 Answer

PS4 Controller Y-axis is linked to L2-button? 0 Answers

What's wrong with OnCollisionEnter? 2 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