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 Ekta-Mehta-D · Oct 25, 2013 at 05:24 AM · rigidbodyposition

Trouble With Positioning Gameobject : Unity3d

Hello everyone ,

I am making 2d game and in that game scene i am translating zombie game object from right to left.

Zombie is having box collider and rigidbody component.

Have a look at my scene :

alt text

Now when they come to the fence means collides with the fence , i want them to position in proper manner as they are not at proper position as you can see in image.

So i have written code to position them in nearest empty position when they collides with fence. But bcz of the rigidbody zombies are not collides with fence hence they dont find empty position.

COde which i have written for zombie is here :

 public var maxSpeed : float ;
 public var minSpeed : float ;
 
 private var x : float;
 private var y : float;
 private var z : float;
 private var prev : float = 0.0;
 private var next : float;
 
 private var currentSpeed : float;
 private var stop : boolean = false;
 private static var fenceEnemy : int;
 private var one : boolean = false;
 private var nearestPosition : GameObject;
 //for glow effect : material alpha color set 105 , 255 ,255 , 255
 
 function Start () {
     SetSpeed();
 }
 
 function Update () {
     
     var amttomove : float = currentSpeed * Time.deltaTime;
     
     if(!stop)
         transform.Translate(Vector3.left * amttomove , Space.World);
 }
 
 function SetSpeed()
 {
     currentSpeed = Random.Range(minSpeed , maxSpeed);    
 }
 
 function OnCollisionEnter(collision : Collision)
 {
     if(collision.gameObject.tag == "Fence")
     {        
         stop = true;
         gameObject.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
         fenceEnemy++;
         nearestPosition = GetNearestTaggedObject();
         gameObject.transform.position = nearestPosition.transform.position;
         nearestPosition.gameObject.tag = "full";
         //gameObject.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
         //yield WaitForSeconds(3.00f);
         //Destroy(gameObject);
         
     }    
     
     if(collision.gameObject.tag == "human")
     {
         
         Physics.IgnoreCollision(collision.gameObject.collider, collider);
     }
     if(collision.gameObject.tag == "zombie")
     {
         Physics.IgnoreCollision(collision.gameObject.collider, collider);
     }
 }
 
 function GetNearestTaggedObject() : GameObject {
     // and finally the actual process for finding the nearest object:
  
     var nearestDistanceSqr = Mathf.Infinity;
     var taggedGameObjects = GameObject.FindGameObjectsWithTag("empty"); 
     var nearestObj : GameObject = null;
  
     // loop through each tagged object, remembering nearest one found
     for (var obj : GameObject in taggedGameObjects) {
  
         var objectPos = obj.transform.position;
         var distanceSqr = (objectPos - transform.position).sqrMagnitude;
  
         if (distanceSqr < nearestDistanceSqr) {
             nearestObj = obj;
             nearestDistanceSqr = distanceSqr;
         }
     } 
     return nearestObj;
 }

So hope you understand the whole situation. if you dnt understand then plz ask me again..

I need your help guys.. Thanks for helping me and supporting me

device-2013-10-24-120042.gif (372.8 kB)
Comment
Add comment · Show 8
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 Ekta-Mehta-D · Oct 25, 2013 at 05:27 AM 0
Share

I have used this logic to position them but if you have any other idea and logic then it will be appreciated.. Thanks..

avatar image yogee · Oct 25, 2013 at 05:57 AM 0
Share

make empty game objects for every zombies in the fence as u want and translate the zombies towards it

avatar image Ekta-Mehta-D · Oct 25, 2013 at 06:08 AM 0
Share

But there will be so many number of zombie in perticular level, and i dont want my zombie to cross fence so i want to arrange zombie in proper manner near fence,.

And my zombies are co$$anonymous$$g from random position..

Here, bcz of the rigidbody of zombie, all zombie are not able to collides with fence, so they are not getting nearest empty position and which does not look gud. If any zombie find other zombie in his way, then they stop translating forward.

Hope you understand.

avatar image yogee · Oct 25, 2013 at 06:15 AM 1
Share

u want to arrange like this: alt text

fence.jpg (7.5 kB)
avatar image Ekta-Mehta-D · Oct 25, 2013 at 06:43 AM 0
Share

yes.. Like this

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by yogee · Oct 25, 2013 at 10:33 AM

if u r generating random zombies means, movie the first zombie towards target (x=100,Y and Z are same),and next one move towards(target+=2) like this for even zombie set target as target+=2 and for odd one target-=2, when colliding with fence stop the movement

alt text


16998-device-2013-10-24-120042.gif (373.6 kB)
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

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

18 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

Related Questions

How do i Clamp the Z position of a rigidbody? 1 Answer

A node in a childnode? 1 Answer

Freeze Position problem 1 Answer

Physics not working properly 1 Answer

How would I make an object disappear after a set amount of time? 3 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