# Spacesim character controller

 0 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 } } `````` more ▼ asked Jul 21 '12 at 02:51 PM Adamant356 7 ● 3 ● 6 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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 more ▼ answered Jul 21 '12 at 07:51 PM Muuskii 1.1k ● 4 ● 10 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. Jul 21 '12 at 08:38 PM Muuskii 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 :) Jul 21 '12 at 09:26 PM Adamant356 Glad I could be of help. Jul 22 '12 at 12:47 AM Muuskii add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 ``````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); } `````` more ▼ answered Jul 21 '12 at 06:21 PM Muuskii 1.1k ● 4 ● 10 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. Jul 21 '12 at 06:32 PM Muuskii 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 :) Jul 21 '12 at 07:28 PM Adamant356 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x1040
x519
x181
x30
x3

asked: Jul 21 '12 at 02:51 PM

Seen: 393 times

Last Updated: Jul 22 '12 at 12:47 AM