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
1
Question by sulas · Jan 27, 2010 at 07:20 PM · transformrandom

moving object to a random location

Hi there,

I am trying to move an object to a random location when something happens.

    transform.eulerAngles=Vector3(0,targetRot,0);
    moveDirection = Vector3(targetX,0,targetZ);
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *=runSpeed;
    transform.Translate(moveDirection*Time.deltaTime);

this makes gets called in the Update function and of course the objects gets those random targetX,targetZ and targetRot variables recreated every frame. This makes the object go into a seizure.

What I would like to do is, create those random values only once and make the object go to that location.

There is also the problem of heading the right direction, random value of targetRot does not necessarily point to the target X and Z. I guess I'll need some math there as well?

so the question are:

-how to create those variables once even though the function is called every frame? -how to set the euler angle of the object correctly to make it face the right direction ?

Thank you

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
3
Best Answer

Answer by duck · Jan 27, 2010 at 08:38 PM

In general, use the Start() function to perform actions that you only want to occur once, when the object first appears. Eg:

var target : Vector3;

function Start() { target = Random.insideUnitSphere * 5; }

function Update() { transform.Translate((target-transform.position)*Time.deltaTime); }

However in your situation, you might want to be able to pick a new random position at any given time, so it might be better to move the random target function, and the code which controls initialisation of the direction to a custom function. You can then call the custom function both in Start() and elsewhere - for instance, in OnMouseUp() or OnCollisionEnter() for example.

In the example below, I'm using Transform.LookAt to make the object face in the direction of the target. I'm also using a different method of moving an object, which calculates the current position by Lerping between the start position and the target position. All good stuff to look up :)

var target : Vector3; var target : Vector3; var timeStarted : float;

function Start() { PickRandomTarget(); }

function PickRandomTarget() { target = Random.insideUnitSphere * 5; transform.LookAt(target); timeStarted = Time.time; startPosition = transform.position; }

function Update() { var i : float = (Time.time-timeStarted); transform.position = Vector3.Lerp(startPosition, target, i ); }

// pick a new target if clicked function OnMouseUp() { PickRandomTarget(); }

Comment
Add comment · Show 1 · 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 ganesh.pingale · Jan 17, 2013 at 08:34 AM 0
Share

what if i want to pause object on collision and change target position after 2 sec then it follow again

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

1 Person is following this question.

avatar image

Related Questions

Random Position Inside an object 1 Answer

random show objects 2 Answers

problem extracting transform from an array 1 Answer

moving random objects to random positions 1 Answer

Smooth look at Random Transform ? 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