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

Physics orientation bug

Discussion in '2D' started by JackAshwell, Nov 22, 2019.

  1. JackAshwell

    JackAshwell

    Joined:
    Oct 21, 2019
    Posts:
    24
    So I'm trying to create a script where the enemy locates the player and automatically move towards them, but I have run into a bit of an error as when I click play, the enemy is orientated wrong.

    My code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyMovement : MonoBehaviour {
    6.    
    7.     public Transform player;
    8.  
    9.     private Rigidbody2D rb;
    10.     private Vector2 movement;
    11.  
    12.     public float moveSpeed;
    13.  
    14.     void Start() {
    15.         rb = this.GetComponent<Rigidbody2D>(); //Gets ridibody2d component of enemy and assigns it to rb
    16.     }
    17.    
    18.     void Update() {
    19.         Vector3 direction = player.position - transform.position; //Finds direction of enemy relative to player
    20.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; //Finds angle of enemy relative to player in degrees
    21.         rb.rotation = angle;
    22.  
    23.         direction.Normalize();
    24.         movement = direction;
    25.     }
    26.  
    27.     private void FixedUpdate() {
    28.         moveCharacter(movement);
    29.     }
    30.     void moveCharacter(Vector2 direction) {
    31.         rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    32.     }
    33. }
    My enemy inspector:
    My player inspector:

    If anyone can find the problem, it would be a great help, thanks!
     

    Attached Files:

  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    A few basics:

    1. Imported models aren't at the orientation you'd expect for a variety of reasons, but thats solved by making the imported model a child so you an offset it in position and rotation as you'd like. Can do it in math too but this is a simple answer to start.

    2. Perhaps just add a rotation in code? I mean, there doesn't seem to a bug here, just lack of extra support code to get the desired behaviour.

    It's probably just the disconnect between 3d package rotation and where the math defaults to.
     
  3. JackAshwell

    JackAshwell

    Joined:
    Oct 21, 2019
    Posts:
    24
    How could i add that rotation then?

    EDIT: I've done it:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyMovement : MonoBehaviour {
    6.    
    7.     public Transform player;
    8.  
    9.     private Rigidbody2D rb;
    10.     private Vector2 movement;
    11.  
    12.     public float moveSpeed;
    13.  
    14.     void Start() {
    15.         rb = this.GetComponent<Rigidbody2D>();
    16.     }
    17.    
    18.     void Update() {
    19.         Vector3 direction = player.position - transform.position;
    20.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    21.         rb.rotation = angle;
    22.  
    23.         transform.LookAt(player.position);
    24.         transform.Rotate(new Vector3(0,90,0),Space.Self);
    25.  
    26.         direction.Normalize();
    27.         movement = direction;
    28.     }
    29.  
    30.     private void FixedUpdate() {
    31.         moveCharacter(movement);
    32.     }
    33.     void moveCharacter(Vector2 direction) {
    34.         rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    35.     }
    36. }
     
    Last edited: Nov 22, 2019
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yep plenty of ways to handle it, and that's one of many. Nice one. You just need to be expecting this sort of thing really, it's not unusual :)