Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AWilliams5 · Apr 25, 2019 at 10:09 PM · rotationailookatkinematicfightingame

How do i get this guy to hold his position?

the ai character is supposed to rotate on the y axis and look at the player's character. The problem i'm having is that i have the ai characters positioned in a particular way.

Position is 6, 0, 15 Rotation is 0, -70, 0 (this rotation is looking forward at the screen aka the player's character) And the scale is all 1.

when i hit play however, my ai character changes to rotation to -158.199 (which is looking the left same direction as the player's character is looking)

How do i fix this so that he holds his rotation while also still following the player's character?

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class LookAt_Player : MonoBehaviour {

 GameObject target;


 // Use this for initialization
 void Start () {

     target = GameObject.Find ("FBX_Box_Model");
 }
 
 // Update is called once per frame
 void Update () {

     Vector3 targetPosition = new Vector3(target.transform.position.x,
         transform.position.y, 
         target.transform.position.z);
     

     transform.LookAt (targetPosition);
     
 }

}

Comment
Add comment · Show 6
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 edthered1009 · Apr 25, 2019 at 11:30 PM 0
Share

Your code looks okay, and I can't find any obvious problems. Are you certain that no other GameObjects besides the player are named "FBX_Box_$$anonymous$$odel"?

avatar image AWilliams5 edthered1009 · Apr 29, 2019 at 01:48 PM 0
Share

Yes. FBX_box_model is the only thing named as such. The ai character changes rotation on play however. It changes from looking at my fbx box model to facing the same direction. $$anonymous$$y ai character rotates when he moves but he isn't already in the right rotation so when my box character moves my ai character is still looking forward but slightly to the left or right depending on how i move my box character. So i want to change the rotation of my ai character so that he can stop changing directions.

avatar image edthered1009 AWilliams5 · Apr 29, 2019 at 02:28 PM 0
Share

This likely won't fix it, but you can replace the obtuse declaration for targetPosition.

 Vector3 targetPosition = target.transform.position;

Or better yet, remove this variable entirely!

 transform.LookAt( target.transform.position );



Show more comments
avatar image ShadyProductions · Apr 29, 2019 at 04:30 PM 0
Share
 var direction = (target.transform.position - transform.position).normalized;
 transform.up = direction;

you might want to change transform.up with your correct direction, (right, forward, etc) you can even slerp it to make it smooth:

 transform.up = Vector3.Slerp(transform.up, direction, Time.deltaTime);
avatar image AWilliams5 ShadyProductions · May 01, 2019 at 10:07 PM 0
Share

thank you for helping but it didn't work.

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kim-Nobre · May 06, 2019 at 02:29 PM

     public GameObject target;
 
     private void Update()
     {
         Vector3 targetDirection = target.transform.position - transform.position;
         targetDirection.y = 0;
 
         transform.rotation = Quaternion.LookRotation(targetDirection);
     }
Comment
Add comment · Show 1 · 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 yugen1 · Apr 26, 2020 at 10:24 PM 0
Share

This isn't helping in my case! Any ideas?

avatar image
0

Answer by Sara_daisy · May 06, 2019 at 11:35 AM

I am also facing the same issue, and i wrote about it in my previous thread. But yeah, i couldn't find any solution for it so i am writing it here. So this thread might get on top again and we might find the right answer. By the way i am currently working on a project for Canada immigration Pakistan, so if anyone needs any information about it let me know. Thanks!

Comment
Add comment · 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
0

Answer by yugen1 · Apr 26, 2020 at 10:24 PM

Hello, Any solution on this? It would be of great help.

Comment
Add comment · 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
0

Answer by edthered1009 · Apr 26, 2020 at 10:30 PM

 NPC.transform.forward = (target.transform.position - NPC.transform.position).normalized;

Simply replace NPC and target with the appropriate GameObjects. If you are setting the NPC's body rotation instead of a separate head axis, simply use this:

 NPC.transform.forward = ((target.transform.position  - Vector3.up * target.transform.position.y) - NPC.transform.position).normalized;

This subtracts the target's Y position from the aiming location, meaning that they will only rotate on the Y-axis instead of on the X. I hope this helps, and please upvote!

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 yugen1 · Apr 26, 2020 at 10:51 PM 0
Share

Thank you very much for your help.

It didn't do the trick in my case and maybe my problem might be different. The gif link trying to explain it.

![Here's a gif to explain the problem][1] [1]: https://media.giphy.com/media/gFUuEDE3hKWG7AOmZ2/giphy.gif

When I hit play, the object changes its rotation and then, from that side, it "follows" or looks at cameras movement. I want the object to look at the camera form the side that its initially positioned (from the back in the gif example)

avatar image edthered1009 yugen1 · Apr 27, 2020 at 12:30 AM 0
Share

So you want it to face away from the camera? If so, you can simply make the assignment negative, like so:

 NPC.transform.forward = -(target.transform.position - NPC.transform.position).normalized;
avatar image yugen1 edthered1009 · Apr 27, 2020 at 09:41 AM 0
Share

I need the following:

Whatever rotation the object has before hitting play, I want it to remain like that, and follow the camera from that angle for x miliseconds (and than return to its initial rotation). The bigger problem I am trying to solve is to simulate a false delay for testing purposes

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

205 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

Related Questions

Simulating Mouse Input for AI 0 Answers

Rotating enemy object to face the player when moving 2 Answers

Look at and animal ai 0 Answers

Is it possible to rotate a child object regardless of the parent rotation ? 1 Answer

Rotate Z-Axis towards center. 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