Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 nockieboy · Jul 17, 2012 at 09:07 PM · arrayvector3relative position

How to move an object and keep its distance relative to the player?

Hi,

I'm trying to create a script that will keep a planet and a sun (both game objects) at a constant distance from the player space ship, so that the player can never reach them. I've tried searching everywhere for a similar script to work from but can't seem to find anything, so I had a go myself. I'm still learning scripting though!

Here's what I've done so far:

 #pragma strict
 
 var isActive : boolean = false;
 
 private var objects : GameObject[];
 private var modLoc : Vector3;
 private var origPos : Vector3;
 var objectLoc = new Array (Vector3(0, 0, 0));
 
 function Start () {
  
  objects = GameObject.FindGameObjectsWithTag("keepDistance");
 
  for (var object in objects) {
  objectLoc.Push(object.transform.position);
  }
  origPos = this.transform.position;
  
 }
 
 function Update () {
  
  if (isActive) {
  modLoc = origPos - this.transform.position;
  //modLoc.z = 0;
  var i = 0;
  var modded : Vector3;
  for (var object : GameObject in objects) {
  modded = objectLoc[i] + modLoc;
  object.transform.position = modded;
  i++;
  }
  }
  
 }


The idea of the script is to store the positions of all objects tagged 'keepDistance' in the objects array. The player's starting position is stored in origPos.

Then, if the script is active, every Update modLoc stores the relative movement of the player and the script iterates through the stored objects array to add modLoc relative movement to them - the idea being that they will stay at a constant distance from the player as they move.

I seem to have hit a wall with arrays (either builtin or Javascript - not sure about either really) and keep getting an error on the following line:

 modded = objectLoc[i] + modLoc;


Unity says "Operator '+' cannot be used with a left hand side of type 'Object' and a right hand side of type 'UnityEngine.Vector3'. Both the objectLoc array and modLoc variable are declared as Vector3, so I'm lost as to why this error is cropping up?

I would appreciate any help whatsoever, especially any amendments that will turn this into a working script!

Thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Winterblood · Jul 17, 2012 at 09:22 PM

I'd recommend placing an empty gameobject at the player position, and parenting all your "distant" objects to that. Then you only need a script on the empty object that updates it's position to match the player, and everything else will update automatically.

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 nockieboy · Jul 17, 2012 at 09:27 PM 0
Share

The simplest solutions are often the best and this is a good one! Wish I'd thought of it myself! ;-) Thanks!

avatar image
0

Answer by fashrista · Feb 12 at 03:41 PM

Have ran into this question and wrote an alternative solution.

 /// <summary>
 /// A class that makes object in 
 /// the background appear very far 
 /// away(sun, moon, planets, mountains) as they keep distance to 
 /// the fromThis object that is holding the camera.
 /// I guess it could be used for other purposes also.
 /// </summary>
 public class KeepDistance : MonoBehaviour
 {
     [SerializeField]
     private GameObject fromThis;
     private Vector3 currentDistance;
     private Vector3 orginaDistance;
     private Vector3 diff;
 
     void Start()
     {
         orginaDistance = transform.position - fromThis.transform.position;
     }
 
     void Update()
     {
         currentDistance = transform.position - fromThis.transform.position;
         diff = orginaDistance - currentDistance;
         transform.position = transform.position + diff;
     }
 }


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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to check if a vector 3 is in a vector 3 array c#. 1 Answer

How can I assign an Array of Vector3 while seeing and editing the values in the Scene? 0 Answers

C# Convert Vector3[] to Vector2[] 3 Answers

changing the camera's local position with Vector3 variables coming from an array 2 Answers

Object follow array of points 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