Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 bwakefield34 · Dec 16, 2018 at 12:47 PM · rotationrigidbodycontrollermousepositionaddtorque

Issue with Rotation behaviour of PID Controller when rotating to look at mouse position

So I've put together my own PID controller from a handful of different scripts I've found to rotate my player towards the mouse location with addTorque and it actually works, kinda.


The issue is that if the mouse is pointing above the player or to the right of the player it won't rotate to that point, but will only rotate towards a similar point on the other side. It's like the script is reading negative coordinates as positive and rotating the rigidbody towards the positive coordinate instead.


I'm not sure thats the whole issue though, as I'm using some error equations and plugging them into the addtorque line at the bottom of the script in a way that doesn't quite make sense to me, I feel like I should only be adding torque on the y-axis, and that torque should be a function of the x and z error values but my brains fried at this point. Just using the xerr or zerr on the y axis of the addTorque line doesn't work either, but I expected that.


Since the description of the problem is terrible here's a gif describing the behaviour: https://media.giphy.com/media/1vZapFYleh0mQmp4sc/giphy.gif


And here's my code:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class PIDController
 {
 
     [Tooltip("Proportional constant (counters current error)")]
     public float Kp = 0.01f;
 
     [Tooltip("Integral constant (counters cumulated error)")]
     public float Ki = 20f;
 
     [Tooltip("Derivative constant (fights oscillation)")]
     public float Kd = 1f;
 
     [Tooltip("Current control value")]
     public float value = 10;
 
     private float lastError;
     private float integral;
 
     public float Update(float error)
     {
         return Update(error, Time.fixedDeltaTime);
     }
 
     public float Update(float error, float dt)
     {
         float derivative = (error - lastError) / dt;
         integral += error * dt;
         lastError = error;
 
         value = Kp * error + Ki * integral + Kd * derivative;
         return value;
     }
 }
 
 public class LookAtController : MonoBehaviour
 {
     Vector3 target;
     Vector3 mousePos;
     Rigidbody rb;
     Vector3 playerPos;
     Vector3 forward;
     float xerrA;
     float zerrA;
     
     public PIDController rotationPID;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     public void FixedUpdate()
     {
 
         Ray ray = Camera.main.ScreenPointToRay(mousePos);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, Mathf.Infinity))
         {
             target = hit.point;
             mousePos.x = Input.mousePosition.x;
             mousePos.y = Input.mousePosition.y;
             playerPos = rb.position;
             forward = rb.transform.forward;
 
             Vector3 cPR = forward;
             Vector3 dPR = (target - playerPos) / (target - playerPos).magnitude;
 
             float xerr = (dPR.x - cPR.x) * 100;
             float zerr = (dPR.z - cPR.z) * 100;
 
                 rb.AddRelativeTorque(rotationPID.Update(xerr), rotationPID.Update(zerr), 0);
         }   
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

155 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

Related Questions

Rigidbody Add Torque with Mouse Drag 0 Answers

how to rotate a Rigidbody to a target rotation using Rigidbody.AddTorque() 0 Answers

AddTorque from position of mouse/touch? 1 Answer

AddTorque to rotate rigidbody to look at a point 3 Answers

How to Convert dir = (a.transform.position - b.transform.position).normalized; to Degrees ? 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