Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 d40bob · Jun 02 at 11:24 PM · movementworldspacelocalspace

How do I make an object move on the world space rather than the local space?

I'm trying to make an object move down a hallway every few seconds, but whenever it moves, I want it to be somewhere random between the walls. so I used a random. Range (-4, 4) to make the object move to somewhere between those two numbers. problem is, every time it moves, the range moves with it, so it can move outside the walls. How do I fix this?? This code does exactly what I want it to, except that it allows the object to move outside the walls of the hallway. void Start() { InvokeRepeating("Tp", 3, 3); } void Tp() { transform.Translate(new Vector3(Random.Range(-4, 4), 0f, 5f);

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 AaronBacon · Jun 03 at 12:13 AM

The problem is just that transform.Translate moves relative to the objects current position. if you set the transform position manually it'll always move to the same world coordinate eg:

 transform.position = new Vector3(Random.Range(-4, 4), 0f, 5f);

However this will only work if the object starts at 0,0,0 in the world, which I'm assuming isn't what you want, so if you do this:

 private Vector3 startPos;
 
 void Start()
 {
    startPos = transform.position;
    InvokeRepeating("Tp", 3, 3);
 }
 
 void Tp()
 {
    transform.position = startPos + new Vector3(Random.Range(-4, 4), 0f, 5f);
 }

It will remember it's starting position and move randomly relative to that Starting Position

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 d40bob · Jun 03 at 06:23 PM 0
Share

Okay, so I did this, but it doesn't seem to constantly move forward. I need it to get closer and closer to the player thats on the other end of the hallway. This code seems to make it move back and forth, but doesn't move it forward every time. How do I get it to move forward 5 units every time the InvokeRepeating goes off?

avatar image Caeser_21 d40bob · Jun 03 at 07:38 PM 0
Share

Set the integers in the the Random.Range() function to something like 1 and 6... This should make it move anywhere between 1 and 5 units in-game every 3 seconds, Hope that helps :)

avatar image d40bob Caeser_21 · Jun 03 at 07:57 PM 0
Share

with this code that Aaron gave me, the object is staying in the hallway, but it's not moving down the hallway. it moves the 5 units once, then just zig-zags in place. so now I have to figure out how to make it move down the hallway with this code.

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

197 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

Related Questions

Box Collider Vertexes in World Space 3 Answers

Raycasting, How to draw a line to the position of the rays end when nothing is hit 1 Answer

How do I get the world space up direction for RotateAround? 0 Answers

Is it possible to constrain an object's rotation in worldspace? 2 Answers

Random.insideUnitSphere doesn't work as expected 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