Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Feb 19, 2016 at 06:31 PM by parisk85 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by parisk85 · Feb 19, 2016 at 04:48 PM · rotationcollision2d gametriggerquaternion

How to move and rotate to other direction when hitting a wall

I have some enemies in my game that i want them moving either horizontally or vertically and changing direction when hitting a wall. (2D game, sprite attached to empty game object and the empty game object has RigidBody2D and Circle Collider).

I want on OnTriggerEnter2D method to change the rotation of my sprite and collider in order to use only one sprite (and now 4 for the different directions) but I cant handle quaternions very well.

I am posting the code of a previous version where I used 4 sprites and changed them according to the direction.

 using UnityEngine;
 using System.Collections;
 
 public class GhostBehaviour : MonoBehaviour {
 
     private enum Direction
     {
         LEFT,
         RIGHT,
         UP,
         DOWN
     }
 
     public float speed;
     private float speedX;
     private float speedY;
 
     private Direction direction;
 
     public Sprite spriteGhostUp;
     public Sprite spriteGhostDown;
     public Sprite spriteGhostLeft;
     public Sprite spriteGhostRight;
 
     // Use this for initialization
     void Start () {
         speedX = 0;
         speedY = 0;
 
         direction = (Direction)Random.Range (0, 4);
 
         switch (direction) {
         case Direction.LEFT:
             speedX = -speed;
             GetComponent<SpriteRenderer>().sprite = spriteGhostLeft;
             break;
         case Direction.RIGHT:
             GetComponent<SpriteRenderer>().sprite = spriteGhostRight;
             speedX = speed;
             break;
         case Direction.UP:
             GetComponent<SpriteRenderer>().sprite = spriteGhostUp;
             speedY = speed;
             break;
         case Direction.DOWN:
             GetComponent<SpriteRenderer>().sprite = spriteGhostDown;
             speedY = -speed;
             break;
         }
     }
 
     // FixedUpdate is called once per frame
     void FixedUpdate(){
         transform.Translate (Time.deltaTime * speedX, Time.deltaTime * speedY, 0.0f);
     }
 
     void OnTriggerEnter2D(Collider2D other){
         if (other.gameObject.tag == "Wall") {
             speedX *= -1;
             speedY *= -1;
 
             switch(direction){
             case Direction.LEFT:
                 direction = Direction.RIGHT;
                 break;
             case Direction.RIGHT:
                 direction = Direction.LEFT;
                 break;
             case Direction.UP:
                 direction = Direction.DOWN;
                 break;
             case Direction.DOWN:
                 direction = Direction.UP;
                 break;
             }
         }
     }
 
     // Update is called once per frame
     void Update () {
         switch (direction) {
         case Direction.LEFT:
             GetComponent<SpriteRenderer>().sprite = spriteGhostLeft;
             break;
         case Direction.RIGHT:
             GetComponent<SpriteRenderer>().sprite = spriteGhostRight;
             break;
         case Direction.UP:
             GetComponent<SpriteRenderer>().sprite = spriteGhostUp;
             break;
         case Direction.DOWN:
             GetComponent<SpriteRenderer>().sprite = spriteGhostDown;        
             break;
         }
     }
 
     public void Freeze(){
         GetComponent<SpriteRenderer> ().color = new Color (0.68f, 1.0f, 1.0f);
         SetSpeed (1);
     }
 
     public void UnFreeze(){
         GetComponent<SpriteRenderer> ().color = new Color (1.0f, 1.0f, 1.0f);
         SetSpeed (4);
     }
 
     void SetSpeed(int s){
         if (speedX > 0) speedX = s;
         if (speedX < 0) speedX = -s;
         if (speedY > 0) speedY = s;
         if (speedY < 0) speedY = -s;
     }
 }

and this is what I came up with this time

public class GhostMovement : MonoBehaviour {

 enum Direction{ UP, DOWN, LEFT, RIGHT};
 private Direction direction;

 public float speed;

 void Start () {
     direction = InitDirection ();
     SetDirection (direction);
     SetRotation (direction);
 }

 void Update () {
     transform.Translate (speed * Time.deltaTime * Vector2.right);
 }

 void OnTriggerEnter2D(Collider2D other){
     if (other.CompareTag ("Boundary")) {
         speed *= -1;
         if (direction == Direction.UP){
             SetDirection (Direction.DOWN);
             SetRotation (Direction.DOWN);
         }
         if (direction == Direction.DOWN){
             SetDirection (Direction.UP);
             SetRotation (Direction.UP);
         }
         if (direction == Direction.LEFT){
             SetDirection (Direction.RIGHT);
             SetRotation (Direction.RIGHT);
         }
         if (direction == Direction.RIGHT){
             SetDirection (Direction.LEFT);
             SetRotation (Direction.LEFT);
         }
     }
     Debug.Log (direction);
 }
 
 Direction InitDirection(){
     System.Array enumArray = System.Enum.GetValues (typeof(Direction));
     return (Direction)enumArray.GetValue (Random.Range (0, enumArray.Length));    
 }     

 void SetDirection(Direction d){
     switch (d){
     case Direction.UP:
         direction = Direction.UP;
         break;
     case Direction.DOWN:
         direction = Direction.DOWN;
         break;
     case Direction.LEFT:
         direction = Direction.LEFT;
         break;
     case Direction.RIGHT:
         direction = Direction.RIGHT;
         break;
     }
 }

 void SetRotation(Direction d){
     switch (d){
     case Direction.UP:
         transform.rotation = Quaternion.Euler(0,0,90);
         break;
     case Direction.DOWN:
         transform.rotation = Quaternion.Euler(180,0,90);
         break;
     case Direction.LEFT:
         transform.rotation = Quaternion.Euler(0,180,0);
         break;
     case Direction.RIGHT:
         transform.rotation = Quaternion.Euler(0,0,0);
         break;
     }

 }

 void ReverseDirection(){
     if (direction == Direction.UP)
         SetDirection (Direction.DOWN);
     if (direction == Direction.DOWN)
         SetDirection (Direction.UP);
     if (direction == Direction.LEFT)
         SetDirection (Direction.RIGHT);
     if (direction == Direction.RIGHT)
         SetDirection (Direction.LEFT);
 }

 void ReverseRotation(){
     if (direction == Direction.UP)
         SetRotation (Direction.DOWN);
     if (direction == Direction.DOWN)
         SetRotation (Direction.UP);
     if (direction == Direction.LEFT)
         SetRotation (Direction.RIGHT);
     if (direction == Direction.RIGHT)
         SetRotation (Direction.LEFT);
 }

}

Please help! Thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by parisk85 · Feb 19, 2016 at 06:31 PM

Problem solved!

 using UnityEngine;
 using System.Collections;
 
 public class GhostMovement : MonoBehaviour {
 
     enum Direction{ UP, DOWN, LEFT, RIGHT};
     private Direction direction;
 
     public float speed;
 
     void Start () {
         direction = new Direction();
         direction = InitDirection ();
         SetDirection (direction);
         SetRotation (direction);
     }
 
     void FixedUpdate () {
         transform.Translate (speed * Time.deltaTime * Vector2.right);
     }
 
     void OnTriggerEnter2D(Collider2D other){
         if (other.CompareTag ("Boundary")) {
             switch (direction){
             case Direction.UP:
                 SetDirection(Direction.DOWN);
                 transform.eulerAngles = new Vector3(180, 0, 90);
                 break;
             case Direction.DOWN:
                 SetDirection(Direction.UP);
                 transform.eulerAngles = new Vector3(0, 0, 90);
                 break;
             case Direction.LEFT:
                 SetDirection(Direction.RIGHT);
                 transform.eulerAngles = new Vector3(0, 0, 0);
                 break;
             case Direction.RIGHT:
                 SetDirection(Direction.LEFT);
                 transform.eulerAngles = new Vector3(0, 180, 0);
                 break;
             }        
         }
     }
     
     Direction InitDirection(){
         System.Array enumArray = System.Enum.GetValues (typeof(Direction));
         return (Direction)enumArray.GetValue (Random.Range (0, enumArray.Length));    
     }     
 
     void SetDirection(Direction d){
         switch (d){
         case Direction.UP:
             direction = Direction.UP;
             verticalSpeed = -speed;
             break;
         case Direction.DOWN:
             direction = Direction.DOWN;
             verticalSpeed = speed;
             break;
         case Direction.LEFT:
             direction = Direction.LEFT;
             horizontalSpeed = -speed;
             break;
         case Direction.RIGHT:
             direction = Direction.RIGHT;
             horizontalSpeed = speed;
             break;
         }
     }
 
     void SetRotation(Direction d){
         switch (d){
         case Direction.UP:
             transform.rotation = Quaternion.Euler(0,0,90);
             break;
         case Direction.DOWN:
             transform.rotation = Quaternion.Euler(180,0,90);
             break;
         case Direction.LEFT:
             transform.rotation = Quaternion.Euler(0,180,0);
             break;
         case Direction.RIGHT:
             transform.rotation = Quaternion.Euler(0,0,0);
             break;
         }
     }
     
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Trying to get bool statement to work in OnTriggerStay2D 1 Answer

How do i make my bullet destroy on trigger? 1 Answer

Rotation (Quaternion.Lerp) not working 0 Answers

Can't seem to get collision to work among my sprites 1 Answer

Smooth look at 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges