- Home /
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
}
}
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
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.
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 :)
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);
}
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.
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
Follow this Question
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