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 bukka · Oct 21, 2014 at 11:29 AM · camerafollow playershake

How to combine the camera that follows the player and the camera shake effect in Unity?

Hi. I need to combine both camera that follows the player and the camera shake effect in the same moment. The problem is in that, if I make the shake effect for the camera, then the camera doesn't follow the player and if I make the camera follow the player, then the shake effect doesn't appear. The basic idea of my script is next:

 Vector3 originalPos;
 GameObject targetPlayer;
 bool canShake;
 void OnEnable()
     {
         originalPos = camTransform.localPosition;
     }
  
     void Update()
     {
             // Camera shakes  
         if (CanShake)
         {
             transform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
         }
             // Camera follows the player
         else
         {
             transform.position = new Vector3(targetPlayer.transform.position.x, transform.position.y, transform.position.z;
         }
     }


Does anyone have ideas how to combine these two things (camera shake effect and camera that follows the player) at the same moment?

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 popuppirate · Oct 21, 2014 at 12:10 PM

Hi mate,

I'm not able to test this but here's my analysis:

   transform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount

This does exactly what you're seeing it does. When CanShake is true, the camera sits at original pos and shakes about. Just off the top of my head, what you might consider doing is putting it as an extra part of your script, so something like this:

     void OnEnable()     
     {  originalPos = camTransform.localPosition;     
     }       
 
 
     void Update()     
     {  
     transform.position = new Vector3(targetPlayer.transform.position.x, transform.position.y, transform.position.z);  //The camera is now always updating position.      
 
 if (CanShake==true)         {         // The shaking occurs if CanShake is true'
     transform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
     } 
 }            

Sorry if this code appears a little dodgy ,but im on Lunch!

Hope this helps,

Popuppirate

Comment
Add comment · Show 3 · 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 bukka · Oct 21, 2014 at 01:55 PM 0
Share

Thank you very much for answer! I'll try to rewrite my code according to your recommendations and then I'll post here my progress))

avatar image bukka · Oct 21, 2014 at 05:12 PM 1
Share

Thank you for a good idea again. I have modified my script and now everything is working cool!!!

avatar image popuppirate · Oct 21, 2014 at 06:17 PM 0
Share

No probs mate- basically for future reference, imagine that everything I want to happen all the time sits outside any condition in the update. I Basically like this:

 void Update(){
     //Stuff I do here happens every frame, so it's good for a camera because the player might move at any given moment.
     
     if(some_condition==true){
     //Stuff I do here happens ONLY if some_condition is true; 
 //if you put in stuff that you want to keep happening all the time here,
 //like updating the camera position, it'll only work when some_condition //is true.  
     } else{
     //The same is to be said with the opposite-all the stuff I put in //here will ONLY happen when some_condition!= true (is not true)
     }
     
     
     }


Hope this helps-don't $$anonymous$$d me if I'm oversimplifying- hope it all works out for you matey!

Cheers,

Popuppirate

avatar image
0

Answer by And1love · Sep 13, 2018 at 08:20 AM

What if for example ...

Look that every cam shake script has a camera original position, maybe just in camshake script we write a ref to a camera folow script and set up the cam shake original cam position to original position on cam follow script. But we also need to remember that in cam folow script we all make in late update func, so in cam shake if we have every frame that will be not logic. cam must know that she follow the player but her function to follow must breaks for a amount of sec for making cam shake function. It maybe like courutines, so we must create a IEnumerator func. So in the end will look like : player equip a sword, than player press key to jump and attack ground from air, than we need an animation trigger in the momment when sword "collide" or touch the ground, this event will coll ienumerator that breaks for a "Ammount of seconds that we choose like CAM SHAKE DURATION" and after this cam follow will continue work... it`s only my logic i`m not too good at programing just 3 mounth work with unity. @bukka

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Shake camera up and down when running? 0 Answers

Camera Movement (One Position to Another) 1 Answer

Positioning Textures on screen 0 Answers

Camera follow help? 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