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 mgreene1 · Mar 08, 2016 at 08:58 AM · positionenemylocalpositionshakegame design

LocalPosition change affecting actual Position?

I have an enemy object that constantly follows the player using A*. I wanted to add a little shake to it for the feedback for an electric weapon to give the impression they were just "shocked." So I parented the SpriteRenderer to an empty transform with the Collider and RB. I added the "shake" script to the sprite object to affect it separately from the RB object.

Problem is, the script ends and I want to move the Sprite back to the Collider object's current position. So I assign the localPosition back to (0,0,0), but it instead transports the entire object (including the collider) back to the (0,0,0) of the scene, as if I had altered the actual Position instead.

The enemies are prefabs which for now are being instantiated via a wave spawner. Here's my code if someone can tell me what's up with this:

private float shakeAmount = 0f; private EnemyAI enemyAIClone; private Transform myRef; private Vector3 zero;

 // Use myRef for initialization
 void Awake()
 {
     enemyAIClone = GetComponentInParent<EnemyAI> ();
     myRef = this.gameObject.GetComponentInChildren<Transform>();
 }
 
 void Update()
 {


     if (Input.GetKeyDown(KeyCode.E)) 
     {
         //Test the shake
         StunShake (0.3f, .3f, true);
     }
 }
 
 public void StunShake (float _amnt, float _leng, bool _isExplosion) 
 {
     if (IsInvoking ("BeginShake") == false || _isExplosion == true) 
     {
         shakeAmount = _amnt;
         zero = new Vector3(0,0,0);
         InvokeRepeating ("BeginShake", 0.01f, .02f);

         if(enemyAIClone.stun == false)
         {
             Invoke ("StopShake", _leng);
         }
     } 
 }
 
 // Update is called once per frame
 void BeginShake () 
 {
     if (shakeAmount > 0) 
     {
         Vector3 myPos = myRef.position;

         
         if (Vector3.Distance(myPos, myRef.position) <= shakeAmount/30) 
         {
             myPos = myRef.position + Random.insideUnitSphere * shakeAmount;
         }

         
         myRef.position = Vector3.Lerp(myRef.position, myPos , 1f);
         //myRef.transform.localPosition = Vector3.Lerp(myPos, Vector3.zero , 1f);

     }
 }
 
 void StopShake () 
 {
     CancelInvoke ("BeginShake");

     zero = myRef.GetComponentInParent<Transform>().position;

     myRef.position = Vector3.Lerp(myRef.position, zero , 1f);
     myRef.localPosition = new Vector3 (0, 0, 0);
 }
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 NoseKills · Mar 08, 2016 at 03:37 PM

Did you check what GetComponentInChildren() returns?

Based on that (without reading all of the code I admit), to me it looks like your 'myRef' is probably the transform of the parent, the one that you don't want to reset to 0,0,0 but now you do.

Another oddity i see is

 myRef.position = Vector3.Lerp(myRef.position, zero , 1f);

That's exactly the same as

 myRef.position = zero;
 

You should check the api docs for what Lerp does also :)

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

Rigidbody local position constraints 2 Answers

Enemy's set position is incorrect? 1 Answer

How to update your position your every frame 1 Answer

Why are my enemies not moving after shaking? 0 Answers

What's a difference between just Scale and localScale or Position and localPosition? 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