Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Update Transform position

Discussion in 'Scripting' started by MrWaaWa, Feb 8, 2016.

  1. MrWaaWa

    MrWaaWa

    Joined:
    Feb 8, 2016
    Posts:
    3
    Hi,
    I have a game where i need both player (2 player) to face the other one.
    My problem is that it doesn't update the enemy position and i don't know how to do.
    Could you give a way to do it or explain me how to ?

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class FaceEnemy : MonoBehaviour {
    6.     public GameObject toFace;
    7.  
    8.     void Start () {}
    9.  
    10.     void Update () {
    11.         float tx = toFace.transform.position.x;
    12.         float tz = toFace.transform.position.z;
    13.         Vector3 target = new Vector3(tx, 1.33f, tz);
    14.         transform.LookAt(target);
    15.     }
    16. }
    17.  
     
  2. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    If you put that script on the enemy and set toFace to be the player it should work.
     
  3. MrWaaWa

    MrWaaWa

    Joined:
    Feb 8, 2016
    Posts:
    3
    Sorry, i didn't explain it well.
    When I said "enemy", it was more like "the other player". It's a 1vs1 game.
    For now, when i launch the game, they are both facing each other, but when they move, they are only facing the position of the other one when the game start.
    Thanks for the reply