Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Adamant356 · Jul 21, 2012 at 02:51 PM · charactercontrollerspacesimulator

Spacesim character controller

Hello,

I have recently begun making a space simulator game, and have had trouble making a character controller that can be used by both the AI and the player. My previous controllers stored the target angle in a quaternion where the ship would gradually turn towards it until the angles match (For players this value would be adjusted using Input.GetAxis).

However, I had difficulty implementing angular acceleration as well as limiting the maximum turning rate of the ship. How would I go about making a controller with these features in JavaScript?

edit:

Here is my code that works properly, but i have had trouble adapting it for the AI. How could I make it work while keeping the same turning speed? Any suggestions are welcome.

  var targetAngleY:float = 0.0; //ship's yaw axis
  var targetAngleX:float = 0.0; //ship's pitch axis
  var targetAngleZ:float = 0.0; //ship's roll axis
  
  var turnspeed:float = 0.25;
  
  var inertiaFactor:float = 92.0;
  
 function Update () {
  Lookat();
 }
 
 function Lookat () {
 
  if (transform.gameObject.tag == "Player") { //if player is piloting
  
  targetAngleY += (Input.GetAxis("Horizontal")) * (turnspeed);
      targetAngleX += (Input.GetAxis("Vertical")) * (turnspeed);
      targetAngleZ += (Input.GetAxis("Roll")) * (turnspeed) ;
      
      targetAngleY *= (inertiaFactor / 100);
      targetAngleX *= (inertiaFactor / 100);
      targetAngleZ *= (inertiaFactor / 100);
      
  transform.Rotate(targetAngleX, targetAngleY, targetAngleZ);
  }
  
  else { //if AI is piloting
  
  } 
 }
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
Best Answer

Answer by Muuskii · Jul 21, 2012 at 07:51 PM

Quaternion.LookRotation is returning your "End-goal" rotation and then we're multiplying that by turn speed. On the other hand the player is giving 3 values between -1 & +1 which we multiply by turnspeed.

Off the top of my head, if you:

 Quaternion LookRotation = Quaternion.LookRotation( targetPos - transform.position);
 
 Vector3 temp = LookRotation.eulerAngles - transform.rotation.eulerangles;
 
 for(int i = 0; i < 3; i++)  //Do this for all three values
 {    
 if(temp[i] < -180)temp[i] += 360;       //don't turn more than 180 degrees
 else if(temp[i] > 180)temp[i] -= 360;   
 
 temp[i] = Mathf.Clamp( temp[i], -1, 1);          //AI output will be between -1 and +1
 }

This will give you a Vector3 with all of it's values between -1 & +1 so you can muliply and use in transform.Rotate.

Thanks

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 Muuskii · Jul 21, 2012 at 08:38 PM 0
Share

Note: You can also divide temp[i] / turnspeed just before we clamp it. This does nothing if temp[i] > turnspeed but otherwise it means the AI slows down enough to stop exactly on the target angle as opposed to continuously overshooting and "vibrating" back and forth.

avatar image Adamant356 · Jul 21, 2012 at 09:26 PM 0
Share

Brilliant, it works! :)

There's also the matter of giving a feeling of weight to the ships again, but I'll mark this as answered and tackle it later.

Thanks for the great help :)

avatar image Muuskii · Jul 22, 2012 at 12:47 AM 0
Share

Glad I could be of help.

avatar image
0

Answer by Muuskii · Jul 21, 2012 at 06:21 PM

 function Lookat () 
 {
  Vector3 targetAngle;
 
  if (transform.gameObject.tag == "Player") { //if player is piloting
 
   targetAngle.y = Input.GetAxis("Horizontal");
   targetAngle.x = Input.GetAxis("Vertical");
   targetAngle.z = Input.GetAxis("Roll");
 
  }
 
  else { //if AI is piloting
 
    AISetsTargetAngle( targetAngle );
 
  } 
 
      targetAngle *= turnspeed;
 
      targetAngle *= (inertiaFactor / 100);   //why is this here?
 
 
  transform.Rotate(targetAngle.x, targetAngle.y, targetAngle.z);
 }
Comment
Add comment · Show 2 · 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 Muuskii · Jul 21, 2012 at 06:32 PM 0
Share

btw: AISetsTargetAngle can be inside another script attached to your ship. . . or any other game object for that matter! Great for having different piloting styles.

avatar image Adamant356 · Jul 21, 2012 at 07:28 PM 0
Share

Thanks for the reply, the code looks much cleaner now :)

Unfortunately things still aren't right: I used the inertiaFactor variable to give a feeling of mass to the ship (0 is sluggish, 100 is frictionless), but now that part of the code doesn't work correctly anymore. I'd rather do away with it entirely and use a new system with variables for angular acceleration and deceleration but can't get my head around it.

Also, how would I go about finding the target angle for the AI? I tried Quaternion.LookRotation but the results aren't right.

Anyway, thanks for your patience :)

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

How to use character controller to push down hinge joint properly 1 Answer

3rd person character movement problems 1 Answer

accessing charactercontroller 1 Answer

Why am I not moving forward? 1 Answer

Ui sidescroller 2d touch ? 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