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 /
  • Help Room /
avatar image
0
Question by tonyjwhite · Oct 25, 2018 at 07:51 AM · movement3dvector3transform.positionlocalposition

Move 3D object on transform.localposition

----Hello there!, I am currently trying to move this object back and forth at set speeds on the Y Axis. I have saved this script for my Enemy Controller, and have it added to several "enemy" 3D objects. Unfortantly the objects do not seem to be moving based on their own local position, but instead are all following the same local position(which I cannot seem to figure out where or what's controlling it). I have them separated using different parents and moving those around, but am still unable to figure out whats wrong.

----The Enemy objects placed at the bottom of my environment are moving far upwards than they should, but they move back and forth as expected.(they go up 5, then bounce -4, +4, -4, +4 FROM their current position.{bounce between 3 and 8 on the Y Axis})

----The Enemy objects placed at a higher elevation in my environment are moving downwards, than stopping, and infinintly vibrating in place.

this is my third time going through the code to see if I could fix it myself, but im still learning, and im clearly having issues with something that feels very simple. Heres a Sample of my Current Code.

 using UnityEngine;
 
 public class MyEnemyController : MonoBehaviour {
 
     public float speed = 3f;            //Speed at which the objects move in Time.deltaTime
     public float speedInc = 1.2f;       //20% increase in speed for objects moving downward
     public float maxHigh = 2f;          //Maximum movement from current position
     public float direction = 1f;        //Rate at which to move
     Vector3 InitialPos;                 //variable for 3D object X,Y,Z
 
     void Start()        {
         //Saving Current Objects coordinates(X,Y,Z) into variable "InitialPos"
         InitialPos = transform.localPosition;   }
     void Update ()      {
         //Saving Maximum Range for objects movement into 2 different variables- one for the Maximum, the other for the Minimum
         float newPositionMax = maxHigh + InitialPos.y;
         float newPositionMin = maxHigh - InitialPos.y;
 
         //If the object is moving in a downward position (were only moving on the Y Axis)
         if(direction == -1){
             //Saving base movement * 20% increase speed & directional movement into a variable "movementYDown"
             float movementYDown = speed * speedInc * Time.deltaTime * direction;
             //Forcing object to move on the Y Axis using Transform.up, and to use the speed and direction input from "movementYDown" variable
             transform.position += transform.up * movementYDown;
         }
         else  //Otherwise If the object is moving in any other direction thats not downward
         {  
             //Saving base movement & directional movement into variable "movementY"
             float movementY = speed * Time.deltaTime * direction;
             //Forcing object to move on the Y Axis using transform.up and to use the speed and direction input from "movementY" variable
             transform.position += transform.up * movementY;             }
 
         //If the Objects current position on Y Axis is "Greater-than" or "Equal-too" the Maximum Heighth we have set
         if(transform.position.y >= newPositionMax)       {
             direction = -1;         //Then move downward in a negative direction
         }
         //Else If the objects current position on Y Axis is "Less-than" or "Equal-too" the Minimum Heighth we have set
         else if(transform.position.y <= newPositionMin)  {
             direction = 1;          //Then move Upward in a positive direction
         }
     }
 }
 
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 tonyjwhite · Oct 25, 2018 at 11:25 AM

I was able to generally fix the problem I was having by added the script onto the parent itself, instead of each enemy--its not specifically what I wanted, but it will work for what I am doing. I did however have to add a new public variable, public float MaxLow = -2f;. and change Line 21 float newposistionMin = MaxHigh - initialPos.y; to float newposistionMin = MaxLow + initialPos.y;

--all the enemies now move exactly how they should, but now theyre all moving at the same time( I was hoping to keep them individual.)

---If anyone can still give me any ideas on how to make it work per object, id really like to know.

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

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

Related Questions

Trying to transform my character's local position on a GetKey. 0 Answers

Reversing movement directions 1 Answer

Move a object along a vector 1 Answer

GameObject doesn't move 1 Answer

Helicopter Move Local Position,Local Position 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