LookAt

Hi
I like to have a gun who detect player and follow it with look at,
I do it but the problem, that the gun no follow the player properly and it change oriantion
Can you explain that to me please.

using UnityEngine;
using System.Collections;

public class ArmesEnnemies : MonoBehaviour {
public GameObject player;
public Transform targetPlayer;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag(“Player”);

}

// Update is called once per frame
void Update () {

transform.LookAt(player.transform.position, Vector3.zero);
}
}

Use code tags when posting code.

–Eric

You can try combining transform.LookAt transform.Translate to make the gun look at the player follow aswell.

transform.LookAt needs the up direction of the world, which you currently have set to Vector3.zero

Use this:

transform.LookAt(player.transform.position, Vector3.up);

You aren’t giving the function an up vector (the second parameter to LookAt)

Or you can just delete your second parameter and it will be the up vector by default.

I’m sorry, I’m a beginner.
The green object is a weapon.
I made a 90 degree rotation
But when I go to “Game” mode, it returns to its original rotation.
The glass object must follow the player.
I reuissit with “Raycast” and the object is movement but it floats.

Put an empty gameobject with your rotated barrel as a child of the empty. Look at wants to point the objects forward vecotr toward the target… your barrel’s forward appears to be one of the sides. So use the empty game object and align your barrel so it properly aligns with the empty game objects forward vector and use the look at on the empty.