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

Need help on converting code for 3D code for 2D.

Discussion in 'Scripting' started by Temp10101, Apr 16, 2015.

  1. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyLook : MonoBehaviour {
    5.     public Transform playerPosition;
    6.     public Quaternion rotationAngleX;
    7.     public float TurnSpeed = 5;
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         rotationAngleX = Quaternion.LookRotation(playerPosition.position - transform.position);
    16.         transform.rotation = Quaternion.Slerp(transform.rotation, rotationAngleX, Time.deltaTime * TurnSpeed);
    17.     }
    18. }
    19.  
    This does work just great, but works on 3D plane, while I work on 2D game. :(
    I just barely worked out to get it working, I seriously have no idea how to get it on 2D plane.
     
  2. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    This code can be achieved by:

    Code (CSharp):
    1.  Quaternion rotation = Quaternion.LookRotation
    2.              (Target.transform.position - transform.position, transform.TransformDirection(Vector3.up));
    3.          transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);