Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Vinnie420 · Mar 23, 2014 at 06:46 AM · instantiaterotatelookatrotatearound

Rotating a GameObject 180 to face another Gameobject on single axis

Hi people, im making a top down game where u can feed fish, the fish get hungry after a certain amount of time and then i can click somewhere to spawn some food and the fish goes and get it, the problem is when i spawn it behind him it swims backwards to the food, so I'm trying to rotate the fish only when its not facing the food. Here's the code im using now: using UnityEngine; using System.Collections;

 public class VisBeweging : MonoBehaviour 
 {
     public float speed;
     public GameObject muntje;
     public GameObject muntje2;
     public GameObject voedsel;
     public bool honger;
     public bool dood;
     public Vector3 richting = Vector3.down;
     public Texture whiteTexture;
     public Vector3 visPos;
 
 
 
 
     void Start ()
     
     {
         Invoke ("HeeftHonger", Random.Range (10,20));   //start heefthonger tussen 15 en 30 seconden
         Invoke ("Grow", 30);                            //start grow na 30 seconden
         Invoke ("Turn", Random.Range (15, 30));            //start turn tussen 15 en 30 seconden
         transform.Rotate(0, 0, 90);                        //draai de prefab 90 graden eenmalig
         speed = Random.Range (2f, 5f);                    //snelheid is random waarde tussen 2 en 5
     }
 
 
     void FixedUpdate ()
     {
 
         visPos = transform.position;                                            //vispostie is plek waar die nu is
 
         if(dood == false && honger == true && VoedselSpawn.voedselCount > 0)    //als hij leeft en honger heeft en er voedsel aanwezig is    
         {
             transform.position = Vector3.MoveTowards(visPos, VoedselScript.foodPos, 10 * Time.deltaTime);    //beweeg naar het voedsel
         }
 
 
 
         if(dood == false)
         {
             transform.Translate (richting * speed * Time.deltaTime); //Beweging van links naar rechts als hij leeft
         }
 
         if(dood == true && transform.position.z < 19)                         //als hij dood is en in het veld 
         {
             richting = Vector3.forward;                                            //beweeg naar boven
             transform.Translate (richting * speed * Time.deltaTime);                
             gameObject.renderer.material.mainTexture = whiteTexture;            //laad witte texture
             gameObject.renderer.material.color = new Color(1, 1, 1, 1);            //kleur word wit
             Invoke ("Destroy", 5);
             if (transform.position.z > 19)                                         //als hij bovenaan komt
             {
                 speed = 0;
                 gameObject.renderer.material.color = new Color(1, 1, 1, 1);        //kleur is boven nog steeds wit
 
             }
         }
 
     
 
     }
 
 
 
 
 
     public void OnTriggerEnter (Collider other)
     {
         if(other.tag == "Grenslijn")                                                        //als je de grenslijn raakt
         {
             transform.RotateAround (transform.position, transform.forward, 180f);            //draai om
         }
 
 
         if(honger == true && other.tag == "Voedsel")                                //als je honger hebt en je raakt voedsel aan
        {
     
             honger = false;                                                            //geen honger meer
             gameObject.renderer.material.color = new Color(1, 1, 1, 1);                //kleur word oranje
             Invoke ("HeeftHonger", Random.Range (15,30));                            // en krijgt tussen 15 en 30 sec weer honger
             CancelInvoke ("Doodanimatie");                                            //hoeft niet meer dood te gaan
             Invoke ("Changetag",0.01f);                                                //verander tag met 0.1 sec vertraging
 
         }
     }
 
 
 
     void Changetag()
     {
         gameObject.tag = "Visje";
 
     }
 
     void Doodanimatie ()
     {     
         dood = true;                                                        
         CancelInvoke ("Spawnmuntje");   
         CancelInvoke ("HeeftHonger");
         CancelInvoke ("Grow");                                                        //hij is dood dus cancel alle timers
         CancelInvoke ("Turn");
         CancelInvoke ("Spawnmuntje2");
 
 
     }
 
 
     void Destroy()
         {
             Destroy (gameObject);
         }
 
 
 
 
     void HeeftHonger ()
     {
         gameObject.renderer.material.color = new Color(0, 1, 0);                // is hij groen
         honger = true;                                                            //heeft honger
         Invoke ("Doodanimatie", 30);                                            //start doodanimatie
         gameObject.tag = "Zieke Vis";                                            //is ziek
 
                                 
     }
 
 
     void Spawnmuntje ()
     {
         Vector3 muntSpawnpos = transform.position;                    //spawnpositie muntje is positie van vis
         muntSpawnpos.z -= 2f;                                        //iets eronder
         muntSpawnpos.y = Random.Range(8,10);                        //random hoogte
         Instantiate (muntje, muntSpawnpos, transform.rotation);       //spawn het muntje
         Invoke ("Spawnmuntje", Random.Range (6,10));                //na 6 - 10 sec weer                  
     }
 
     void Grow ()
     {
         transform.localScale = new Vector3(1.5f, 1.5f, 1.5f);                //word 1.5x zo groot
         Invoke ("Spawnmuntje", Random.Range (6,10));                        //start spawnen van grijze muntje
         Invoke ("Grow2", 60);                                                //groei na 60 seconden weer
 
     
     }
 
 
     void Grow2 ()
     {
         transform.localScale = new Vector3 (2, 2, 2);                    //word 2x zo groot
         CancelInvoke("Spawnmuntje");                                    //cancel spawn grijze muntje
         Invoke ("Spawnmuntje2", Random.Range (6,10));                    //spawn gouden muntje
 
     }
 
     void Spawnmuntje2 ()
     {
         Vector3 muntSpawnpos = transform.position;                        //spawnpositie muntje is positie van vis
         muntSpawnpos.z -= 10f;                                            //iets eronder
         muntSpawnpos.y = Random.Range (8, 10);                            //random hoogte
         Instantiate (muntje2, transform.position, transform.rotation);   //spawn het gouden muntje
         Invoke ("Spawnmuntje2", Random.Range (6,10));                    //na 6 - 10 sec weer    
     }
 
 
     void Turn ()
     {
         transform.RotateAround (transform.position, transform.forward, 180f);        //draai om
         Invoke ("Turn", Random.Range (15, 30));                                        //draai weer om tussen 15 en 30 seconden
     }
 
         
 }
 
 
 
Comment
Add comment · Show 2
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
avatar image Dave-Hampson ♦♦ · Mar 26, 2014 at 11:15 AM 0
Share

I think you want to look into AI behaviours, moving towards a point: http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-flee-and-arrival--gamedev-1303

avatar image abi-kr01 · Mar 27, 2014 at 05:00 AM 0
Share

u can give a try to lookat function

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate to Center of Screen 2 Answers

LookAt() and RotateAround() 1 Answer

Instantiated Object - Look At. 1 Answer

Simple rotations question 2 Answers

360° Roation in one min 1 Answer


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