Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 Dorscherl · Jul 03, 2021 at 10:49 PM · rotationailookatmouselooklookattarget

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.)

Comment
Add comment · Show 2
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 logicandchaos · Jul 04, 2021 at 06:08 PM 0
Share

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.

avatar image Dorscherl logicandchaos · Jul 05, 2021 at 06:43 AM 0
Share

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

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

232 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 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

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


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