Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 ClassicCorey · Jul 20, 2015 at 03:57 PM · camerarotationfpsquaternionscriptingbasics

Storing camera angles for a FPS

I am making a First Person Shooter as sort of an introduction to programming. I have separate scripts. one handles movement and camera rotation, one handles spawning the bullets and applying a force to them, and the last handles specifics of shooting a bullet.

However, I would like to make it so whenever a bullet is instantiated, the camera rotates simulating knock-back in a way; but I'm not quite sure how to do that. Here is my code:

 void Update () {
     Quaternion x = Quaternion.Euler (Input.GetAxis ("Mouse X"));
     Quaternion y = Quaternion.Euler (Input.GetAxis ("Mouse Y" + 3));
     if(Input.GetButton ("Fire1")){
         Camera c = Camera.main;
         verticalRotation = Input.GetAxis ("Mouse Y");
         c.transform.localRotation.Quaternion.Euler(x,y,0);
     }

}

In this, I'm trying to store the camera rotation in Quaternion angles, and add 3 to the camera's vertical rotation nudging it up a bit. Is this a viable way of doing such a thing and I'm just missing something, or should I go a completely different route?

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 334499p · Jul 20, 2015 at 09:23 PM

This method gets a little bit jittery so what I do is parent the main camera to an empty GameObject. Use that gameObject as the "camera" for your character camera script and then use the actual camera to handle all extraneous camera motions.

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 ClassicCorey · Jul 21, 2015 at 08:11 AM 0
Share

Oh. That's a nice idea to try out. Another idea I came across was to attach an animation to the camera, making an animation to just shake it. Is there a tutorial I could glimpse at to help me do this with my camera?

avatar image 334499p · Jul 21, 2015 at 06:00 PM 0
Share

Animating this wouldn't allow for randomness of the recoil effect, because of that there aren't tutorials out there for that approach. But if you want to use that approach then you need to still parent the cam to an empty gameobject since basic animations override scripts. So you need to create the animation and change its mode to legacy then use the following script on the camera:

 Animation anim;
 Void Start(){
 anim = GetComponent<Animation>();
 }
 
 Void Recoil(){
 if(anim.isPlaying()){
 anim.CrossFade("gunRecoilAnimation");
 }else{
 anim.Play();
 }
 }


$$anonymous$$ake sure the animation on the camera is also set to one shot and dont play it on start.

avatar image ClassicCorey · Jul 21, 2015 at 09:23 PM 0
Share

Thanks for the answers! I'll start working on that.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I properly rotate an FPS camera with a joystick? 1 Answer

Checking for quaternion values to not be NaN 4 Answers

First Person Shooter Help With Cam 1 Answer

Camera Rotation Question 1 Answer

how to clamp y axis with Quaternion.Euler in unity 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