- Home /
Simulating Mouse Input for AI
I'm trying to create an AI controller for my character. On my character is an "eyes" transform that would rotate and would be where the first person camera would attach itself to when the player controls it.
The character has a Vector2 field called LookInput and simply tells the eyes transform when to rotate. The player controller passes in mouse input and sensitivity to LookInput. Yay, that much works but I need a way to simulate mouse input that AI can compute when trying to look at a target. I want to impose the same restrictions on AI as I would a player (clamping how much they can look up or down etc.)
can you share your code? I'm guessing that the mouse input it takes is a vector2 of it's position? if so to simulate it you could make a vector2 mouseAi; and supply that for the coordinates, however getting the right coordinates is another story.. perhaps you could use a ray co$$anonymous$$g out from the character's face that may need a world to screen point conversion.
I use a condition based system for my character controllers in my games, that way it's easy to swap in computer or Ai control schemes as well as add in different automatic behaviours.
So my lookInput Vector2 is assigned euler angle values on awake and every update I add the mouse axis and sensitivity to it like so.
//Awake
Vector3 eulerAngles = transform.eulerAngles;
LookInput = new Vector2(eulerAngles.y, eulerAngles.x);
//Update
LookInput.x += Input.GetAxis("Mouse X") * rotationSensitivity.x;
LookInput.y= ClampAngle(y - Input.GetAxis("Mouse Y") * rotationSensitivity.y, yMinLimit, yMaxLimit);
//Calculating rotation
eyeRotation = Quaternion.AngleAxis(LookInput.x, Vector3.up) * Quaternion.AngleAxis(LookInput.y, Vector3.right);
r = Quaternion.FromToRotation(lastUp, transform.up) * r;
eyeRotation = r * eyeRotation;
lastUp = transform.up;
//Assigning rotation
eyeTransform.rotation = eyeRotation;
This much works great for mouse input and controllers and even allows me to rotate the character and maintain relative rotation so that looking down always looks at the feet.
So I either need to have it so the AI can replicate the mouse axis to look where it needs or allow the AI to set it's eyes rotation to view the target with transform.lookat. Bad thing about the second option is I have no way (that I know of) to ensure the eyes would react similarly to the player input. I don't want any rolling of the object if it's upside down and I need the clamping so my IK wont have weird behaviours
Your answer
Follow this Question
Related Questions
Look at and animal ai 0 Answers
NavMesh agent won't rotate around Y axis 0 Answers
Follower Airplane problem 1 Answer
Alternatives to LookAt() -1 Answers
Gimbal-Lock with LookAt, RotateTowards, and LookRotation 0 Answers