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
1
Question by xenonsin · May 17, 2014 at 01:25 AM · cameravector3perlin noiseshake

Camera Shake while following Player?

Currently I have this script attached to an empty game object which follows the player around. The camera is also a child of this object.

 using UnityEngine;
 using System.Collections;
 
 public class CameraShake : MonoBehaviour
 {
     public float duration = 0.5f;
     public float speed = 5.0f;
     public float magnitude = 0.2f;
 
     void Update()
     {
         if (Input.GetKeyDown("f"))
         {
             PlayShake();
         }
     }
 
     //This function is used outside (or inside) the script
     public void PlayShake()
     {
         StopAllCoroutines();
         StartCoroutine("Shake");
 
 
     }
 
     private IEnumerator Shake()
     {
         float elapsed = 0.0f;
 
         Vector3 originalCamPos = transform.position;
       
         Debug.Log(originalCamPos);
 
         float randomStart = Random.Range(-1000.0f, 1000.0f);
 
         while (elapsed < duration)
         {
             elapsed += Time.deltaTime;
 
             float percentComplete = elapsed / duration;
 
             float damper = 1.0f - Mathf.Clamp(1.5f * percentComplete - 1.0f, 0.0f, 1.0f);
             float alpha = randomStart + speed * percentComplete;
 
             float x = Mathf.PerlinNoise(alpha, 0.0f) * 2.0f - 1.0f;
             float z = Mathf.PerlinNoise(0.0f, alpha) * 2.0f - 1.0f;
 
 
 
             x *= magnitude * damper;
             z *= magnitude * damper;
 
             x += originalCamPos.x;
             z += originalCamPos.z;
 
 
             transform.position = new Vector3(x, originalCamPos.y, z);
 
             yield return 0;
         }
 
         transform.position = Vector3.Lerp(transform.position, originalCamPos, Time.deltaTime * 5f);
     }
 }


However, as the player moves, while the camera is shaking, it doesn't follow the player. It only updates its position after the shake has ended. I know the error is when grabbing the original position and lerping to that after the shake, but I'm not sure how to update that to the player's position. Any thoughts?

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
3
Best Answer

Answer by Jeff-Kesselman · May 17, 2014 at 01:32 AM

Im not sure why you are doing this in a co-routine. It would be much simpler and less obtuse just to have a shake component that jitters the camera in its Update.

BUT doing it this way you will have to acquire the camera's current position at the start of the while loop because thats where the code restarts after your yield.

Comment
Add comment · Show 2 · 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 xenonsin · May 17, 2014 at 01:39 AM 0
Share

Wow, what a simple fix. Thanks for pointing it out.

I used a co-routine because I wanted to control the duration using a while loop. I'm not sure if it's the best practice, but it's what came into $$anonymous$$d.

If i would implement this in Update, would I need some sort of time stamp system in place?

avatar image Jeff-Kesselman · May 17, 2014 at 01:46 AM 1
Share

Yes, so typically when you are doing timed things in Update you keep some kind of time value in a class field.

For instance, for an effect that yo unwanted to end in 3 seconds...

 public class TimedEffect:$$anonymous$$onoBehaviour{
 
      private float effectTimer= 0;
 
      public void StartEffect(){
           effectTimer = 3;
      }
 
      void Update(){
          if (effectTimer>0){
               effectTimer -= Time.deltaTime;
               // increment effect below....
          }
      }
 }

In general, when working with a game engine, it is better to use the game engine's game loop then to create your own loops on the side.

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

21 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

Related Questions

Convert type UnityEngine.Vector3' to float' Error 1 Answer

How to get the forward vector normal to the camera's forward vector regardless of camera pitch 1 Answer

Tracking Vertical Angle between Camera and Object 0 Answers

Keep camera at certain distance from character when rotating 3 Answers

Is it possible to shake the camera? 4 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