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 metal_invader · Nov 16, 2012 at 07:03 PM · instantiatetransformspawnmovetranslate

How to move a prefab to a gameobject position?

I've been using this following code that a professor of mine wrote a while ago. And it worked pretty well until today. The way it works is that the objects with this script move towards another object (set by the user, it could be an empty game object or anything). However, when using this script today, it moves the destination object that the objects with the script are supposed to be moving towards up on the Y-axis against my will. Then the destination sinks down as though it has a rigidbody (it doesn't). I have the objects that are supposed to move instatiating every 10 seconds in increments of 5. Anyways, is there a better script or a way to fix this? Here's the code:

 var waypoints : Transform [];
 var speed = 6;  // dependant on object mass.  might need to increase if mass > 1
 var closeDistance = 1;  // can set how close is close
 var goback = false; // go back to the first location if true (looping like)
 var flag : Transform;  // use to visualize next destination
 
 // 
 private var index = 0;
 private var destiny : Transform;
 private var vspeed = 0.0;
 
 private var controller : CharacterController;
 
 function
 Start()
 {
     controller = GetComponent(CharacterController);
     
     destiny = null;
     if (waypoints.Length > 0)
     {
         destiny = waypoints[0];
         destiny.position.y = transform.position.y;
         transform.LookAt(destiny);
         if (flag) flag.position = destiny.position;
         Debug.Log("Destiny " + destiny.position);
     }
     if (destiny == null)
     {
         Debug.Log("NO WAYPOINTS SET");
     }
     vspeed = speed;  // set it to the main speed
 }
 
 function
 GetNextLocation()
 {
     if (destiny && CloseEnough())
     {
         
         if (index >= waypoints.Length)
         {
             if (goback)
             {
                 index = 0;
                 vspeed = speed;
             }
             else {
                 vspeed = 0;  // we stop moving
                 return;
             }
         }
         
         destiny = waypoints[index++];
 
         if (flag) flag.position = destiny.position;
 
 
     }
     if (destiny) {
         transform.LookAt(destiny);
         //Debug.Log ("Look At " + destiny.position);
     }
     
 }
 
 function
 CloseEnough()
 {
     var sqrLen = (destiny.position - transform.position).sqrMagnitude;
 
    if( sqrLen < closeDistance*closeDistance )
    {
       return true;
     }
     
     return false;
 }
 
 
 function Update () {
 
     GetNextLocation();
     
     controller.SimpleMove(transform.TransformDirection(Vector3.forward) * vspeed);
 }
 
 function
 LateUpdate()
 {
         if (destiny)
         destiny.position.y = transform.position.y;
 }
 
 @script RequireComponent(CharacterController)
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

Answer by Loius · Nov 16, 2012 at 08:22 PM

destiny.position.y = transform.position.y;

^ that line looks like it's the one moving your targets around.

It looks like this script is designed to work on a flat plane.

I think what you want to do, instead of altering destiny's position, is create a new position equal to (transform.position but with .y = destiny.position.y), and LookAt that new position instead of directly at destiny.

Comment
Add comment · Show 6 · 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 metal_invader · Nov 30, 2012 at 05:35 PM 0
Share

Unfortunately deleting all instances of transfor$$anonymous$$g the object's position seems to not resolve the problem. The gameobject they are moving towards still gets throttled into the air and falls as though it has gravity.

avatar image metal_invader · Nov 30, 2012 at 05:44 PM 0
Share

also, how would I do the whole "lookat" with the new position in code? I actually don't know how to move objects and give them mass at the same time so they collide with objects

avatar image Loius · Nov 30, 2012 at 08:07 PM 0
Share

var pos : Vector3 = Vector3( destiny.position.x, transform.position.y, destiny.position.z );

transform.LookAt( pos );

Check the settings on your target object, make sure it doesn't have a moving rigidbody attached. If you remove all the "destiny{*} =" lines from this code, it won't move them around, but something else might be.

avatar image metal_invader · Nov 30, 2012 at 11:34 PM 0
Share

that code just gives me the error "NullReferenceException: Object reference not set to an instance of an object"

avatar image metal_invader · Nov 30, 2012 at 11:36 PM 0
Share

however, it DOES work (in that they do not get launched into the air now)

Show more comments

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

10 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

Related Questions

instantiate,destroy,gain speed in time 2 Answers

Can't move instantiated prefab 2 Answers

Instantiate VS Transform 1 Answer

how do i know where my instantiated object came from? 2 Answers

Move object forward 1 unit then stop 2 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