Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 bitthebillias · May 10, 2021 at 07:16 PM · camera-movementcamera rotatecamera-lookcamera followcamera movement

Automatic 3rd person camera

I am trying to make a 'tank control style game and am working on the camera. I would like for it to be automatic like in older games.

Here is what I am working with:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class Main_Camera_Controller : MonoBehaviour
 {
     public GameObject Camera;
     public Vector3 _offset;
     public Vector3 rawInputMovement;
     public float speed;
     [Range(0.01f, 1.0f)]
     public float SmoothFactor;
 
     private void Start()
     {
         _offset = Camera.transform.position - this.transform.position;
     }
    void LateUpdate()
     {
 
         if (rawInputMovement.x < 0)
         {
             Quaternion camTurnAngle = Quaternion.AngleAxis(transform.rotation.y * speed * Time.deltaTime, Vector3.up);
             _offset = camTurnAngle * _offset;
 
         }
         else if (rawInputMovement.x > 0)
         {
             Quaternion camTurnAngle = Quaternion.AngleAxis(-transform.rotation.y * speed * Time.deltaTime, Vector3.down);
             _offset = camTurnAngle * _offset;
         }
         else
         {
             // get camera to be centered on the player again somehow...
 
         }
         Vector3 newPosHorizontal = transform.position + _offset;
         Camera.transform.position = Vector3.Lerp(Camera.transform.position, newPosHorizontal, SmoothFactor/Time.deltaTime);
         Camera.transform.LookAt(this.transform);
     }
     public void onMovement(InputAction.CallbackContext value)
     {
         Vector2 inputMovement = value.ReadValue<Vector2>();
         rawInputMovement = new Vector3(inputMovement.x, 0, inputMovement.y);
     }
 
 }

I am not sure as to how to get the camera to move back behind the player when moving forward.

The way I am doing it is dependant on the player's rotation at the moment and probably not the best approach? I originally had it set by using the input values from a gamepad but this seemed to be more akin to what I am aiming for.

I am not sure; this is my first dive into how cameras work. Any info would be appreciated.

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

Answer by bitthebillias · May 11, 2021 at 09:23 PM

just posting this for others to find useful.

This is my solution for a tank-like automatic camera:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class Main_Camera_Controller : MonoBehaviour
 {
     public GameObject Camera;
     public Transform DefaultPos;
     public Vector3 _offset;
     public Vector3 rawInputMovement;
     public float speed;
     [Range(0.01f, 5.0f)]
     public float SmoothFactor;
     public bool Collided;
 
     private void Start()
     {
         _offset = Camera.transform.position - this.transform.position;
     }
     private void LateUpdate()
     {
         Vector3 CamRotation = _offset;
         if(rawInputMovement.x < 0)
         {
             Quaternion camTurnAngle = Quaternion.AngleAxis(rawInputMovement.x * speed * Time.deltaTime, Vector3.up);
             CamRotation = camTurnAngle * CamRotation;
         }
         if (rawInputMovement.x > 0)
         {
             Quaternion camTurnAngle = Quaternion.AngleAxis(-rawInputMovement.x * speed * Time.deltaTime, Vector3.down);
             CamRotation = camTurnAngle * CamRotation;
         }
         if (Collided)
         {
             //cam pos to collision point
         }
         else
         {
             Camera.transform.position = Vector3.Lerp(Camera.transform.position, DefaultPos.position, SmoothFactor * Time.deltaTime);
         }
         Camera.transform.LookAt(transform);
     }
     public void onMovement(InputAction.CallbackContext value)
     {
         Vector2 inputMovement = value.ReadValue<Vector2>();
         rawInputMovement = new Vector3(inputMovement.x, 0, inputMovement.y);
     }
 }


Basically, you have a point(DefaultPos) that the camera(Camera) is always trying to reach via lerping

The rotation comes in by calculating the offset then multiplying the current camera rotation angle by the offset. (I am no expert on this by any means) This makes it sway the direction you are rotating in.

Comment
Add comment · Show 1 · 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 bitthebillias · May 11, 2021 at 09:24 PM 0
Share

there are definitely other ways of doing this I'm sure but this is just a jumpoff point.

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

125 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

Related Questions

Mouse Look Help 2 Answers

My camera is not linking with my movement 0 Answers

Automatic Camera Rotation in the 3D Game Kit 0 Answers

Camera following player gameObject,Camera follows player, with script attached. 1 Answer

How to look player and enemy at the same time using cinemachine FreeLook camera. 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