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 /
avatar image
0
Question by jollyroger901 · Jan 08, 2020 at 09:34 AM · scripting problemmovement scriptaxis3d model

making models move correctly

I'm trying to apply a code I followed online to a model with different axis (original fish faced in the z axis, new fish faces in the -x axis). using the code below the original fish works fine. when I use it on the new fish however, the fish just move sideways awkwardly. I changed the speed vector to the x axis and the fish move properly, but after that they don't seem to communicate to one another. They just swim around the boundary in a big circle. I'm positive it involves the axis somehow, but I don't know enough about coding to fix it. any suggestions?

fish movement:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FishMovement : MonoBehaviour
 {
     public FishManager myManager;
     float speed;
     bool turning = false;
 
     // Start is called before the first frame update
     void Start()
     {
         speed = Random.Range(myManager.minSpeed, myManager.maxSpeed);
     }
 
     // Update is called once per frame
     void Update()
     {
         //determine the bounding box of the manager cube
         Bounds b = new Bounds(myManager.transform.position, myManager.swimLimits * 2);
 
         //if fish is outside the bounds of the cube or about to hit something
         // then start turning around
         RaycastHit hit;
         Vector3 direction = Vector3.zero;
 
         if (!b.Contains(transform.position)) 
         {
             turning = true;
            
             direction = myManager.transform.position - transform.position;
 
         }
         else if (Physics.Raycast(transform.position, this.transform.forward * 50, out hit))
         {
             turning = true;
             direction = Vector3.Reflect(this.transform.forward, hit.normal);
         }
         else
         {
             turning = false; 
         }
 
         if (turning)
         {
             // turn towards the center of the manager cube
            
             transform.rotation = Quaternion.Slerp(transform.rotation,
                                                     Quaternion.LookRotation(direction),
                                                     myManager.rotationSpeed * Time.deltaTime);
         }
 
         else
         {
             if (Random.Range(0, 100) < 10)
             {
                 ApplyRules();
             }
         }
         
         // fish movement
         transform.Translate(0, 0, speed * Time.deltaTime);
     }
 
     void ApplyRules()
     {
         GameObject[] gos;
         gos = myManager.fishy;
 
         Vector3 vcentre = Vector3.zero;
         Vector3 vavoid = Vector3.zero;
         float gSpeed = 0.01f;
         float nDistance;
         int groupSize = 0;
 
         foreach(GameObject go in gos)
         {
             if(go != this.gameObject)
             {
                 nDistance = Vector3.Distance(go.transform.position, this.transform.position);
                 if (nDistance <= myManager.neightbourDistance)
                 {
                     vcentre += go.transform.position;
                     groupSize++;
 
                     if(nDistance < 1.0f)
                     {
                         vavoid = vavoid + (this.transform.position - go.transform.position);
                     }
 
                     FishMovement anotherFlock = go.GetComponent<FishMovement>();
                     gSpeed = gSpeed + anotherFlock.speed;
                 }
             }
         }
 
         if (groupSize > 0)
         {
             vcentre = vcentre / groupSize + (myManager.goalPos - this.transform.position);
             speed = gSpeed / groupSize;
 
             Vector3 direction = (vcentre + vavoid) - transform.position;
             if (direction != Vector3.zero)
                 transform.rotation = Quaternion.Slerp(transform.rotation,
                                                       Quaternion.LookRotation(direction),
                                                       myManager.rotationSpeed * Time.deltaTime);
             
         }
     }
 }

Fish Manager:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FishManager : MonoBehaviour
 {
     public GameObject FishPrefab;
     public float speed = 5.0f;
     private int numberOfFish = 50;
     public GameObject[] fishy;
     public Vector3 swimLimits = new Vector3(5, 5, 5);
     public Vector3 goalPos;
 
     [Header("Fish Settings")]
     [Range(0.0f, 5.0f)]
     public float minSpeed;
     [Range(0.0f, 5.0f)]
     public float maxSpeed;
     [Range(1.0f, 10.0f)]
     public float neightbourDistance;
     [Range(0.0f, 5.0f)]
     public float rotationSpeed;
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         fishy = new GameObject[numberOfFish];
 
        for(int i = 0; i < numberOfFish; i++)
        {
             Vector3 pos = this.transform.position + new Vector3(Random.Range(-swimLimits.x, swimLimits.x),
                                                                 Random.Range(-swimLimits.y, swimLimits.y),
                                                                 Random.Range(-swimLimits.z, swimLimits.z));
 
             fishy[i] = (GameObject)Instantiate(FishPrefab, pos, Quaternion.identity);
             fishy[i].GetComponent<FishMovement>().myManager = this;
        }
         goalPos = this.transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
         if(Random.Range(0,100) < 10)
         goalPos = this.transform.position + new Vector3(Random.Range(-swimLimits.x, swimLimits.x),
                                                         Random.Range(-swimLimits.y, swimLimits.y),
                                                         Random.Range(-swimLimits.z, swimLimits.z));
 
     }
 
     
 
 
 }
 

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

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Ady_M · Jan 08, 2020 at 11:51 AM

If you cannot fix the models permanently in a 3D modelling tool like Blender, Maya, etc. then one workaround is to create an empty gameobject (per fish), place the model inside the empty gameobject and rotate the model in the Scene view so the head of the fish is aligned with the empty gameobject's Z axis.

Attach your scripts to the empty gameobject instead of the model.

If you decide to experiment with rigidbodies later the rigidbody should be on the empty gameobject.

Comment
Add comment · Show 3 · 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
avatar image jollyroger901 · Jan 09, 2020 at 01:28 AM 0
Share

that was explained to me before and I was hoping to use a different strategy before I went with that option. I wouldn't say its a wrong one but I fell as though its just being lazy. as if I chose the silly answer over a better, stronger one that wasn't directly in front of me, you know?

avatar image Ady_M jollyroger901 · Jan 09, 2020 at 02:03 AM 0
Share

If you're talking about solving the problem with an empty gameobject as a parent then yes, I completely understand what you mean; it's really not the "correct way". If you want to do it correctly you should fix the models.

avatar image jollyroger901 Ady_M · Jan 26, 2020 at 06:45 PM 0
Share

Thanks for the help. Appreciate it.

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

212 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 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 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 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 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 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 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 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 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 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

W key to move doesnt work,Movement Script doesnt do anything when i press w 0 Answers

Player Sliding without any Input 2 Answers

2D Movement with axis? 1 Answer

Stop movement while attacking 1 Answer

New to Unity & Scripting, How Do I Create a Movement System Thats About Building Up Speed? 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