- Home /
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);
}
}
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"?
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.
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 );
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);
thank you for helping but it didn't work.
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);
}
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!
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!
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)
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;
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
![](https://koobas.hobune.stream/wayback/20220612200226im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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