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 astracat111 · Nov 07, 2016 at 07:41 AM · camera-movementcamera movement

Adjusting Z value of Child Cam in Unity3Ds Follow Camera prefab? (FreeLookCam.cs)

I've been using Unity3ds standard Free Look Cam, and it's been working great so far for what I need, but I'm having a hard time implementing a zoom.

From what I'm understanding, the parent Camera's Y rotation moves...then the child from that is the Pivot object and that's X position moves...and then the child of that is the child Camera object, the camera that's used, and that one's Z determines the distance that the camera follows.

When the game starts, I'm not really wrapping my head around why you can't simply adjust the Z value of the child Camera that's used without it messing up and losing it's lock on it's object that it's supposed to focus on.

If I hit "play mode" and just manually enter in a different Z position value for the child camera, it works perfectly, and yet I can't put this code in the child cam's update method...nor am I finding where I should put it or how I should put it in the following code.

Ultimately, I'm trying to implement a camera movement based zoom, and trying to stay away from field of view zoom.

Here's just some test code that I was trying to put inside a script attached to the child camera:

 void Update() { 
     transform.position = new Vector3(0,0,-7);
 }

Without it somehow interfering with FreeLookCam.cs's HandleRotationMovement method (I think that's what's happening)...

 using System;
 using UnityEngine;
 using UnityStandardAssets.CrossPlatformInput;

 namespace UnityStandardAssets.Cameras
 {
 public class FreeLookCam : PivotBasedCameraRig
 {
     // This script is designed to be placed on the root object of a camera rig,
     // comprising 3 gameobjects, each parented to the next:

     //     Camera Rig
     //         Pivot
     //             Camera

     private int MainCameraChildID;

     [SerializeField] private float m_MoveSpeed = 1f;                      // How fast the rig will move to keep up with the target's position.
     [Range(0f, 10f)] [SerializeField] private float m_TurnSpeed = 1.5f;   // How fast the rig will rotate from user input.
     [SerializeField] private float m_TurnSmoothing = 0.0f;                // How much smoothing to apply to the turn input, to reduce mouse-turn jerkiness
     [SerializeField] private float m_TiltMax = 75f;                       // The maximum value of the x axis rotation of the pivot.
     [SerializeField] private float m_TiltMin = 45f;                       // The minimum value of the x axis rotation of the pivot.
     [SerializeField] private bool m_LockCursor = false;                   // Whether the cursor should be hidden and locked.
     [SerializeField] private bool m_VerticalAutoReturn = false;           // set wether or not the vertical axis should auto return

     private float m_LookAngle;                    // The rig's y axis rotation.
     private float m_TiltAngle;                    // The pivot's x axis rotation.
     private const float k_LookDistance = 100f;    // How far in front of the pivot the character's look target is.
     private Vector3 m_PivotEulers;
     private Quaternion m_PivotTargetRot;
     private Quaternion m_TransformTargetRot;

     private float CamXp = 0;
     private float CamYp = 0;
     private float CamZp = -2;
     public float cameraDistanceMax = 5f;
     public float cameraDistanceMin = -1f;
     public float cameraDistance = 2f;
     public float scrollSpeed = 2.4f;

     protected override void Awake()
     {
         base.Awake();
         // Lock or unlock the cursor.
         Cursor.lockState = m_LockCursor ? CursorLockMode.Locked : CursorLockMode.None;
         Cursor.visible = !m_LockCursor;
         m_PivotEulers = m_Pivot.rotation.eulerAngles;

         m_PivotTargetRot = m_Pivot.transform.localRotation;
         m_TransformTargetRot = transform.localRotation;
     }


     protected void Update()
     {
 
         HandleRotationMovement();
         if (m_LockCursor && Input.GetMouseButtonUp(0))
         {
             Cursor.lockState = m_LockCursor ? CursorLockMode.Locked : CursorLockMode.None;
             Cursor.visible = !m_LockCursor;
         }

     }

     private void OnDisable()
     {
         Cursor.lockState = CursorLockMode.None;
         Cursor.visible = true;
     }


     protected override void FollowTarget(float deltaTime)
     {
         if (m_Target == null) return;

         // Move the rig towards target position.
         transform.position = Vector3.Lerp(transform.position, m_Target.position, deltaTime*m_MoveSpeed);
     }


     private void HandleRotationMovement()
     {
         if(Time.timeScale < float.Epsilon)
         return;

         // Read the user input
         var x = CrossPlatformInputManager.GetAxis("Mouse X");
         var y = CrossPlatformInputManager.GetAxis("Mouse Y");

         // Adjust the look angle by an amount proportional to the turn speed and horizontal input.
         m_LookAngle += x*m_TurnSpeed;

         // Rotate the rig (the root object) around Y axis only:
         m_TransformTargetRot = Quaternion.Euler(0f, m_LookAngle, 0f);

         // on platforms with a mouse, we adjust the current angle based on Y mouse input and turn speed
         m_TiltAngle -= y*m_TurnSpeed;
         // and make sure the new value is within the tilt range
         m_TiltAngle = Mathf.Clamp(m_TiltAngle, -m_TiltMin, m_TiltMax);
         

         // Tilt input around X is applied to the pivot (the child of this object)
         m_PivotTargetRot = Quaternion.Euler(m_TiltAngle, m_PivotEulers.y , m_PivotEulers.z);

         if (m_TurnSmoothing > 0)
         {
             m_Pivot.localRotation = Quaternion.Slerp(m_Pivot.localRotation, m_PivotTargetRot, m_TurnSmoothing * Time.deltaTime);
             transform.localRotation = Quaternion.Slerp(transform.localRotation, m_TransformTargetRot, m_TurnSmoothing * Time.deltaTime);
         }
         else
         {
             m_Pivot.localRotation = m_PivotTargetRot;
             transform.localRotation = m_TransformTargetRot;
         }
     }
 }
 }
Comment
Add comment · Show 1
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 astracat111 · Nov 07, 2016 at 02:54 AM 0
Share

I found the answer. You just add this to LateUpdate() { } or Update() { } of the wall clip script:

 float mouseScrollSpeed = 5f;
 m_OriginalDist += Input.GetAxisRaw ("$$anonymous$$ouse ScrollWheel") * mouseScrollSpeed;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dannark · Jan 02, 2017 at 08:25 AM

Don't touch on the camera Transform directly because the game needs to use this Z value in order to do not collide with the walls If you want to zoom in and zoom out just use the closestDistance variable from ProtectCameraFromWallClip.cs script. (This is in the same camera-gameobject), ajust the value for something around 30 or 50. example:

     public void controllVehicle(){
         ...
         //Ajust camera position
         pivot.transform.localPosition = new Vector3 (0, 15, 0);
         pivot.transform.parent.GetComponent<FreeLookCam> ().SetTarget(this.gameObject.transform.parent);
         pivot.transform.parent.GetComponent<ProtectCameraFromWallClip> ().closestDistance = 50f;
     }
 
 
     public void positionCameraBack(){
         pivot.transform.localPosition = new Vector3 (0, 1, 0);
         pivot.transform.parent.GetComponent<FreeLookCam> ().SetTarget(playerTrans);
         pivot.transform.parent.GetComponent<ProtectCameraFromWallClip> ().closestDistance = 0f;
     }
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

Character and Camera motion smooth in Editor, but jitters in Build,Character and Camera motion are smooth in Editor, but jittery after build 0 Answers

Making camera follow upwards 3 Answers

Help make camera zoom smoother 3 Answers

Need help updating camera position to go above the player 2 Answers

How to make custom transitions between virtual 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