Is it at all possible to use transform.LookAt or Quaternion.LookRotation to rotate an object to look at another object on one axis only?
I’ve done searches of the forums and read over 100 posts related to this, but none of the solutions really works right. Specifically, all I’m trying to do is rotate an object smoothly about it’s Y-Axis over time to point at another object.
Oh, and by the way, many thanks to andeeee who has time and time again posted explanations on variations of this topic. They have come close, but still aren’t working quite right. Even so, reading through his posts have taught me oodles of other things that I didn’t know that will help me later:)
In that case, you do need Quaternion.LookRotation. Store the object’s current rotation and then use LookRotation to make a “target” rotation. You can then interpolate between the two smoothly using Quaternion.Slerp.
I’m glad I happened to find this thread instead of having to start a new one (first post here, btw!).
I’m fairly new to Unity, and I’m trying to figure out how to make Diablo-like movement system. It doesn’t have to be anything too complicated: just click somewhere, and the character goes there.
Don’t worry about the cubes being spawned, I was just playing around… Anyhow, now that I somehow managed to get the blue “character” cube to face the direction the mouse is being clicked at, I need the cube to actually move to that point till it reaches it.
I suppose the target coordinates can be found from the raytraced RaycastHit-object (RaycastHit.point?), but with what kind of function I’d move the cube to its destination?
I guess it’s something really easy, I just haven’t figured it out yet (transform.Translate…?)
I am glad I found this thread…and I am trying to figure out how to do this:
What I am wanting to do is create the point/click movement style, like that of Archlord or WoW. Basically, I point to a spot on the screen and the character turns and goes to that point where you clicked, then stops.
I am wanting to remove the WASD key movements and just go with the mouse point/click.
I don’t know how to set this up, where to begin, nothing…any help would be greatly appreciated. Thanks in advance.
Sorry to resurrect this thread, but it seems you guys know what you’re doing. I’m trying to make the top of a stationary turret turn in a 2D Platformer by using LookAt but instead of aiming the turret at the character, it aims the top of the turret at him. I want it to move smoothly as well but meanstreak’s code isn’t working. My turret just turns all over the place. This is my turret script with meanstreak’s code instead of LookAt:
var ShootSpeed : int = 1;
var fire : boolean = false;
var IdleRotateSpeed : int = 2;
var Bullet : GameObject;
var Idle : boolean = true;
var target : Transform;
var BulletSpeed : int = 1000;
function OnTriggerEnter (other:Collider) {
if(other.gameObject.CompareTag("Player"))
{
Idle = false;
fire = true;
target = other.gameObject.GetComponent(Transform);
}
}
function OnTriggerStay (other:Collider) {
if(other.gameObject.CompareTag("Player")) {
var newRotation = Quaternion.LookRotation(transform.position - target.position, Vector3.forward);
newRotation.x = 0.0;
newRotation.z = 0.0;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 8);
if(fire) {
Fire();
}
}
}
function OnTriggerExit (other:Collider) {
if(other.gameObject.CompareTag("Player"))
{
Idle = true;
fire = false;
}
}
function Update () {
if(Idle) {
idle();
}
}
function Fire () {
fire = false;
var shot = Instantiate (Bullet, transform.position, transform.rotation);
shot.rigidbody.AddForce (transform.forward * BulletSpeed);
yield WaitForSeconds(ShootSpeed);
fire = true;
}
function idle () {
print ("Doing nothing");
}
Any help is appreciated thanks. And by the way, I’m not using all of the variables yet, I’m trying to get this to work first.
Ive seen this thread before and its suggestion for setting the values you dont want to rotate to zeros. It never worked for me. I would end up as you say, with the rotation pretty much going all over the show.
The best approach is just to rotate around the axis you want to change… ie.
I will use this for enemy units in a 3rd person action adventure with platforms which have different heights.
Normally my units would simple lean forward or backward with their whole body if you were on a different height.
Now that problem won’t be there.
Thanks to above posters, this has been a thorn in my eye for quite some time.
Don’t blat over the X and Z components of a quaternion - instead, take the target vector, blat its Y component, and then pass it into LookRotation.
var flatVectorToTarget = transform.position - target.position;
flatVectorToTarget.y = 0;
var newRotation = Quaternion.LookRotation(flatVectorToTarget);
... Slerp as before ...
ok here is a simple fix based on andeeeee Method, how ever, that method did not calculate for your Ai being on uneven terrain (Hills etc). by changing point.y = 0.0f; to point.y = transform.position.y; makes the (Ai) look forward no matter what, use IK perhaps to make the top half of the body or head look towards the player. good luck all =)
and thanks to andeeeee for helping me battle this one my self for a while =)
public Transform target;
var point = target.position;
point.y = transform.position.y;
transform.LookAt(point);
K I know this is old, but I just had to say that this saved my life. And I wanted to give back to the community. This is the code I used to disable 1 axis using the LookAt function. I recommend the moderators pin this since I’m sure many games are going to have turrets in them
public GameObject target_con;
Vector3 target_point;
// Update is called once per frame
void Update ()
{
target_point = target_con.transform.position;
target_point.y = 0;
//transform.LookAt(target_con.transform.position,transform.up);
transform.LookAt(target_point);
}
This disables the object from being able to look up and down but will still follow the target left and right.
In the inspector you drag your target into the ‘target_con’ variable.