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

Cannot rotate 2Dobjects

Discussion in 'Editor & General Support' started by Sacrifice429, Sep 22, 2017.

  1. Sacrifice429

    Sacrifice429

    Joined:
    Feb 28, 2016
    Posts:
    4
    First time coder attempting to make a 2D Tower Defense game. The enemies spawn, but do not rotate. In the tutorial I am learning from (https://www.raywenderlich.com/107529/unity-tower-defense-tutorial-part-2) they mention child sprites being apart of this while providing you with the enemies. I wanted to do this from the ground up and so here we are! Considering I have no error codes... I can't figure it out!


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MoveEnemy_A : MonoBehaviour {
    6.  
    7.  
    8.     [HideInInspector]
    9.     public GameObject[] waypoints_A;
    10.     private int currentWaypoint = 0;
    11.     private float lastWaypointSwitchTime;
    12.     public float speed = 1.0f;
    13.  
    14.     void Start()
    15.     {
    16.         lastWaypointSwitchTime = Time.time;
    17.     }
    18.  
    19.  
    20.     void Update() {
    21.         transform.Translate (0, 0, Time.deltaTime * 1);
    22.  
    23.  
    24.  
    25.         //1
    26.         Vector3 startPosition = waypoints_A [currentWaypoint].transform.position;
    27.         Vector3 endPosition = waypoints_A [currentWaypoint + 1].transform.position;
    28.         //2
    29.         float pathLength = Vector3.Distance (startPosition, endPosition);
    30.         float totalTimeForPath = pathLength / speed;
    31.         float currentTimeOnPath = Time.time - lastWaypointSwitchTime;
    32.         gameObject.transform.position = Vector3.Lerp (startPosition, endPosition, currentTimeOnPath / totalTimeForPath);
    33.         //3
    34.         if (gameObject.transform.position.Equals (endPosition)) {
    35.             if (currentWaypoint < waypoints_A.Length - 2) {
    36.                 // 3.a
    37.                 currentWaypoint++;
    38.                 lastWaypointSwitchTime = Time.time;
    39.  
    40.                 RotateIntoMoveDirection();
    41.  
    42.             } else {
    43.                 //3.b
    44.                 Destroy(gameObject);
    45.                 //TODO: deduct health
    46.             }
    47.         }
    48.     }
    49.     // rotate enemies
    50.  
    51.     private void RotateIntoMoveDirection() {
    52.         //1
    53.         Vector3 newStartPosition = waypoints_A [currentWaypoint].transform.position;
    54.         Vector3 newEndPosition = waypoints_A [currentWaypoint + 1].transform.position;
    55.         Vector3 newDirection = (newEndPosition - newStartPosition);
    56.         //2
    57.         float x = newDirection.x;
    58.         float y = newDirection.y;
    59.         float rotationAngle = Mathf.Atan2 (y, x) * 180 / Mathf.PI;
    60.         //3
    61.         GameObject sprite = (GameObject)
    62.             gameObject.transform.FindChild("Sprite").gameObject;
    63.         sprite.transform.rotation =
    64.             Quaternion.AngleAxis(rotationAngle, Vector3.forward);
    65.     }
    66.  
    67. }
    68.  
     
  2. Sacrifice429

    Sacrifice429

    Joined:
    Feb 28, 2016
    Posts:
    4