Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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