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 the_megajoey · Mar 15, 2016 at 11:07 PM · c#camerathird-person

making a camera stay at the right distance (c#)

I have this script that lets me rotate a camera around a target using the mouse, but when the target moves (this is for a third person controller/camera) the minimum distance between the character and the camera get longer.

How do i stop the camera from going further away from the character? see the screenshots below for an example and the code.

the distance to begin with:

alt text

and after moving for a while:

alt text

the script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerCameraScript : MonoBehaviour 
 {
     public GameObject target;
     private float speedMod = 10.0f;
     private Vector3 point;
     
     void Start () 
     {
 
     }
     
     void Update () 
     {
         point = target.transform.position;
 
         float mouseV = Input.GetAxis ("Mouse X");
         float mouseH = Input.GetAxis ("Mouse Y");
 
         transform.RotateAround (point,new Vector3(mouseH,mouseV,0.0f),20 * Time.deltaTime * speedMod);
         transform.LookAt(point);
     }
 }

to recap I want the camera to stay at the same distance from the character all the time and not zoom out like in the second screenshot.

screenshot039.jpg (15.1 kB)
screenshot040.jpg (17.2 kB)
Comment
Add comment · Show 3
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 Cherno · Mar 16, 2016 at 09:42 AM 0
Share

After the transform.LookAt line, insert this line:

  transform.position = transform.position - target.transform.position  * 10f;
avatar image LazyElephant Cherno · Mar 16, 2016 at 09:56 AM 0
Share

Are you sure that line will work as is? Unless I'm missing something, you would have to add parentheses around the subtraction or else the order of operations will cause the multiplication to be evaluated first. Also, without normalizing the vector subtraction, it doesn't look like it will maintain a constant distance from the target.

Correct me if I'm wrong, but I believe the following changes are necessary:

  transform.position = (transform.position - target.transform.position).normalized * 10f;

avatar image Cherno LazyElephant · Mar 16, 2016 at 10:33 AM 0
Share

Yes, you are right. I forgot about the order.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Graphics_Dev · Mar 16, 2016 at 12:18 PM

Here is a clean solution for camera follow. Add a camera as a child of the player located to have the exact view you want. Disable the camera component on this object. Add another camera to your scene and add this script. Drag the other camera to it's transform property in the inspector. Hit play and watch the magic!

Use this code:

 using UnityEngine;
 using System.Collections;
 
 public class SmoothCamFollow : MonoBehaviour 
 {
     public Transform target;
     private new Transform camera;
 
     public float posSpeed = 1.0F;
     public float rotSpeed = 1.0F;
 
     // Use this for initialization
     void Start () 
     {
         camera = GetComponent<Transform> ();
     }
 
     // Update is called once per frame
     void Update () 
     {
         // position movement
         camera.position = Vector3.Lerp (camera.position, target.position, (posSpeed * Time.deltaTime));
 
         // rotation movement
         camera.rotation = Quaternion.Lerp (camera.rotation, target.rotation, (rotSpeed * Time.deltaTime));
     }
 }
 
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 the_megajoey · Mar 16, 2016 at 06:21 PM 0
Share

What do you mean by drag the camera to its transform property? I tried dragging it into the transform component in the inspector but none of the fields would accept it.

avatar image Graphics_Dev the_megajoey · Mar 16, 2016 at 06:42 PM 0
Share

When you put the script on the non-parented camera, there will be a transform slot on the script ;)

avatar image the_megajoey Graphics_Dev · Mar 16, 2016 at 06:55 PM 0
Share

D'oh! course there is! cheers!

avatar image
1

Answer by ComradeVanti · Mar 16, 2016 at 10:12 AM

the_megajoey

Just make the camera a child of a child of you player model:

Player --> Somechild--> Camera

If you rotate Somechild the camera will rotate around your gameobject.

So Somechild would have the coords 0,0,0 relative to your player. The Camera would have some coords depending on what distance you want.

Good luck

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
avatar image
0

Answer by 5c4r3cr0w · Mar 17, 2016 at 03:16 PM

I guess this is something that you need.

http://wiki.unity3d.com/index.php?title=MouseOrbitImproved.

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

117 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

Related Questions

Multiple clip exceptions for a camera. 1 Answer

Rotating Camera around player object 0 Answers

How to pan camera based on mouse position 0 Answers

Multiple Cars not working 1 Answer

3rd Person Camera & Controller C# 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