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 B1uEM4oM4o · Feb 09, 2012 at 01:40 PM · objectrandomize

Constantly randomize and update object's position

Hi,

I am trying to write a simple wandering AI script. Basically I have decided to make a box the target of my character, and when he reaches the box, the box will pick a new random position on the map within a set area. Here is my script:

using UnityEngine; using System.Collections;

public class SelfSpawnning : MonoBehaviour {

 private int posX; // positions for waypoint object
 private int posY;
 private int posZ;
 
 public Vector3 pos;
 
 public Transform waypoint; // box that the character will seek
 
 public bool reachedWaypoint; // whether charater reached the waypoint
 // if reached it, randomize new position for waypoint
 
 void Awake(){
     
     reachedWaypoint = false;

     SeedPos(); // seeds initial position for waypoint
     
     waypoint.position = pos;

     Instantiate(waypoint);
 }

 // Use this for initialization
 void Start () {
     reachedWaypoint = false;
 }
 
 // Update is called once per frame
 void Update () {
     if (reachedWaypoint) // do we need to find a new pos for the waypoint?
         SeedPos(); // if so we seed new position
     
 }
 
 void SeedPos(){
     posX = Random.Range(-25,  25);
     posY = 5;
     posZ = Random.Range(-25,  25);
     pos = new Vector3(posX, posY, posZ);
     
     waypoint.transform.position = new Vector3(posX, posY, posZ);
     
     reachedWaypoint=false;

 }

}

It runs error free (all you need to do is drag some transform into the public var area for "waypoint". However, I want the position to change each time you make the reachedWaypoint variable true. The Vector3 pos generates new coordinates by calling SeedPos(); but the position of the waypoint doesnt change. If I try to go waypoint.position.x = posX, I get an error, saying I can't modify that value for some reason.

How can I go about making my object move and what is the reason for that error? Thanks for any input, much appreciated.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gabs · Feb 09, 2012 at 01:53 PM

To modify the position, you can't do:

transform.position.x = value;

You need to store the position in a Vec3 first:

 Vector3 p = transform.position;
 p.x = value;
 transform.position = p;

That's because the "transform.position" is a getter (not a publicly exposed variable), so you can't set it's child values like that.

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
avatar image
0

Answer by B1uEM4oM4o · Feb 10, 2012 at 07:59 AM

Ok, thanks, that makes sense now, I'll try that. Appreciated!

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
avatar image
0

Answer by B1uEM4oM4o · Feb 10, 2012 at 07:59 AM

Ok, I'm still having a bit of trouble. I did what you suggested,

 void SeedPos(){
     
     pos = waypoint.position;
     pos.x = Random.Range(-25,  25);
     waypoint.position = pos;
 

}

I can call this function by checking a bool in the inspector. I can see the Vector3 positions changing in the inspector too since they are public but the position of the transform doesn't update for some reason. Why would this be? Note the script is attached to the main camera, and it instantiates a prefab, waypoint, and moves it with the above function. Why doesn't it update and move instead of just changing the values?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Move character's position to middle of object 1 Answer

Unity Serializer Scene Only? 1 Answer

getcomponent' is not a member of 'object' 2 Answers

How to call OnGUI() on class object 1 Answer

Object spins out of control 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