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 /
avatar image
0
Question by hudzterz · Aug 22, 2019 at 12:39 PM · camerascripting problemscript.fps

Free Look Camera Help

Hello, I'm finding a difficult time having a free look camera work with my script. Essentially I'm making an FPS game similar to ARMA III, and I want a certain key, e.g: Z, to allow the camera to move around while the player movement stays in the same direction. All other methods researched did not work with my script. The camera look code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CamMouseLook : MonoBehaviour
 {
     Vector2 mouseLook;
     Vector2 smoothV;
     public float sensitivity = 5.0f;
     public float smoothing = 2.0f;
     public float negativeYClamp = -65f;
     public float positiveYClamp = 80f;
     private bool freeLookCheck = false;
     GameObject character;
 
     // Start is called before the first frame update
     void Start()
     {
         character = this.transform.parent.gameObject;
     }
 
     // Update is called once per frame
     void Update()
     {
         var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
 
         md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
         smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
         smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
         mouseLook += smoothV;
         mouseLook.y = Mathf.Clamp(mouseLook.y, negativeYClamp, positiveYClamp);
             
         character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
         transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);        
       
     }
 }


The camera is also parented to the Player. (Player is parent). Is this the problem?

I have tried methods such as using a bool to represent whether freeLookCheck is true/false, false being normal rotation and true being freeCam rotation, etc.

Thank you for your time and help.

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 sacredgeometry · Aug 22, 2019 at 08:41 PM

Nesting GameObjects might give you this functionality with less leg work.

Video

Note:

You could also nest a transform and just move the camera to it instead of using two cameras.

Example

 public class CameraController : MonoBehaviour
 {
     public Transform FreeLookTransform;
     public float FreeLookRotationSpeed = 20.0f;
     public Camera FPSCamera;
     public Camera FreeLookCamera;
     
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Space))
         {
            SwitchCamera();
         }
 
         CameraMovement();
     }
 
     void SwitchCamera()
     {
         FPSCamera.enabled = !FPSCamera.enabled;
         FreeLookCamera.enabled = !FreeLookCamera.enabled;
     }
 
     void CameraMovement()
     {
         if(FreeLookCamera.enabled)
         {
             // Do Free Look Rotation on 
             var horizontal = Input.GetAxis("Mouse X");
             FreeLookTransform.Rotate(new Vector3(0, horizontal ,0) * FreeLookRotationSpeed);
 
         }
         else 
         {
             // This would be your normal FPS mouse movement
         }
     }
 }

alt text


screenshot-2019-08-22-at-220854.png (266.7 kB)
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 hudzterz · Aug 23, 2019 at 01:39 PM 0
Share

Really sorry, but I'm confused at your explanation, could you explain what 'nesting' is?. I was also not planning on using two cameras but was hoping that I could somehow lock the player rotation while allowing the camera to look around. (I can of course create a clamp on the x axis for the camera while this occurs. The Y-Axis already has a clamp as seen before.) Could I use the Rigidbody freeze rotation for this?

P.S: I forgot to mention I'm new to Unity, but I have been learning for about a month, so I still a fairly basic explanation. Thanks for helping. P.P.S The video was very kind of you. Thanks.

avatar image sacredgeometry hudzterz · Aug 23, 2019 at 01:43 PM 0
Share

Hi there. Nesting is having one thing inside another thing in this case GameObjects.

The video shows exactly what I mean I can make another showing it working but my script does everything except the normal FPS looking.

Player movement would just be handled by a different script and wouldn't need to change.

i.e. you would still be able to move forwards and backwards and strait from left to right its just what forwards means will be locked into the last position because the Free look camera is no longer changing the players orientation.

You dont need a clamp to do this.

avatar image sacredgeometry sacredgeometry · Aug 23, 2019 at 01:44 PM 0
Share

Let me do the whole implementation so you can see it working and then upload another video explaining it more simply.

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

277 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 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

Character controller (3d) don´t move whith a moving platform. 1 Answer

How can I switch between two cameras ? 1 Answer

FPS recoil 1 Answer

How do I tilt my camera when I'm doing a certain action 1 Answer

How can i track/update the lastPosition of a camera when switching between cameras ? 0 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