• 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 SVGK · Feb 08, 2014 at 08:35 PM · cameracontrol

How to make camera position relative to a specific target.

i'm in the process of making a camera controller component for the player, the idea is that it stays fixed on the player and can be turned left and right with the Q and E keys, and zoomed in with the mouse scroll wheel, however, the script i have set up works in every way but one, the left and right part, see, the camera rotates perfectly fine, but for whatever reason, the camera flies out a massive distance from the ball unless the ball is at 0 on both the X and Z axis:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour
 {
     public Transform target;
 
     public float distance;
     public float lift;
     public float XPosition;
 
     public bool cameraTurnIsFixed = false;
 
     void Update ()
     {
        transform.position = target.position + new Vector3(XPosition, lift, distance);
        transform.LookAt(target);
 
        if (Input.GetAxis("Mouse ScrollWheel") < 0 && distance >= -5)//Zoom out
        {
          distance -= 0.2f;
        }
        if (Input.GetAxis("Mouse ScrollWheel") > 0 && distance <= -2.5)//Zoom in
        {
          distance += 0.2f;
        }
 
        if (cameraTurnIsFixed == false)
        {
           if (Input.GetKey (KeyCode.Q))
           {
               transform.RotateAround(target.position, Vector3.up, 40 * Time.deltaTime);
               distance = transform.position.z;
               XPosition = transform.position.x;
           }
 
             if (Input.GetKey (KeyCode.E))
           {
           transform.RotateAround(target.position, -Vector3.up, 40 * Time.deltaTime);
           distance = transform.position.z;
           XPosition = transform.position.x;
           }
        }
     }
 }

to fix this, i need to make the xPosition and distance variables relative to the target (the player ball), however, i don't know how to do this to be honest, therefore i am asking here what i should do.

and please, don't mistake this as me asking to "make my script for me", i just need a nudge in the right direction, and in fact it would be more helpful for me because i would actually know how it works, just being handed a script with no explanation or informative comments doesn't exactly teach me anything.

cameraTurnIsFixed is a thing i have in place for when i don't want the player to run the camera in certain places.

the player is a rolling sphere, the camera is meant to be situated about 1.5 units above the sphere, to get a view from above to make the game feel more 3D, the distance is a short distance the camera is from the player, all the while, the player should be able to rotate the camera on the horizontal plane, almost like if it were a planet, and there was a sun around 1.5 units above the player.

monster hunter has a camera that functions like this too, though you can set the height of the camera, and the player isn't a sphere.

so can someone assist me?, perhaps point out some documentation and where to apply it to or something.

Comment
Add comment · Show 8
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 SVGK · Feb 09, 2014 at 03:36 PM 0
Share

i still need help, i tried modifying the distance (the Y axis) along with the cameraTurning (X) but that didn't work either, i'm trying to get the camera to be turnable in a perfect circular orbit around the player on the horizontal plane.

am i looking at this the wrong way?.

avatar image SVGK · Feb 09, 2014 at 08:18 PM 0
Share

i'm still in need of assistance, tried messing with rotations, no joy.

avatar image getyour411 · Feb 09, 2014 at 08:27 PM 0
Share

I haven't made a 3rd person cam from scratch (kudos for trying) but I do have, I$$anonymous$$HO, the best 3rd person mmo-style zoom/rotate/orbit camera script I've found to date posted here:

http://www.getyour411.com/unoob/player.html

Scroll down to the mmo-cam style camera- maybe looking at how someone else did it will help you adopt what you are wanting.

avatar image DajBuzi · Feb 09, 2014 at 08:49 PM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/Transform.RotateAround.html

Try this method.

avatar image SVGK · Feb 10, 2014 at 12:13 PM 0
Share

thanks to both of you, personally DajBuzis answer seems more helpful as the other page is a bit hard to follow, i'm trying it out right now, it isn't working, the distance and lift variables are resetting the camera position constantly.

this line right here does it:

 transform.position = target.position + new Vector3(cameraTurning, lift, distance);

the whole script is like this:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : $$anonymous$$onoBehaviour
 {
     public Transform target;
     public float distance;
     public float lift;
     public bool cameraTurnIsFixed = false;
 
     void Update ()
     {
         transform.position = target.position + new Vector3(0, lift, distance);
         transform.LookAt (target);
 
         if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") < 0)
         {
             distance -= 0.2f;
         }
         if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") > 0)
         {
             distance += 0.2f;
         }
 
         if (cameraTurnIsFixed == false)
         {
             if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Q))
             {
                 transform.RotateAround(target.position, Vector3.up, 40 * Time.deltaTime);
             }
             
             if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.E))
             {
                 transform.RotateAround(target.position, -Vector3.up, 40 * Time.deltaTime);
             }
         }
     }
 }

i'm not entirely sure how to fix it, as i need the camera to follow the player and stay a set distance above them, parenting wont work because the player is a sphere.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sumeetkhobare · Feb 10, 2014 at 08:13 PM

Hey, i figured the flaw in your code, i guess. The update function continuously asks your camera to point at the target, whereas it wants to look someplace else while staying at (0,lift,distance), right? Do something like this.


    if(cameraTurnIsFixed == true)
        transform.LookAt (target);
    else
    {
        if (Input.GetKey (KeyCode.Q))
        {
            transform.RotateAround(target.position, Vector3.up, 40 * Time.deltaTime);
        }
        if (Input.GetKey (KeyCode.E))
        {
            transform.RotateAround(target.position, -Vector3.up, 40 * Time.deltaTime);
        }
    }

or if you want to like orbit your camera around your player while looking at it only, not anywhere else, you could do like this:


    transform.position = target.position + target.up *lift - target.forward * distance;
    transform.LookAt(target);
the distance vector is minus, because the camera will be behind the player, if you add them it will be in front of it looking at its face. Correct me if i am wrong, if not and this works, mark the question solved. :)
Comment
Add comment · Show 13 · 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 SVGK · Feb 10, 2014 at 09:22 PM 0
Share

that code's a bit messed up, like a lot, even after clearing up the errors with the general placement of the {} things, the first set makes the camera turn rather than rotate around the player, and the second one just does crazy spinning shtiz.

i think there may have been some miscommunication here on my part, i didn't explain my code clearly enough, see, cameraTurnIsFixed is a thing i have in place for when i don't want the player to run the camera in certain places.

the player is a rolling sphere, the camera is meant to be situated about 1.5 units above the sphere, to get a view from above to make the game feel more 3D, the distance is a short distance the camera is from the player, all the while, the player should be able to rotate the camera on the horizontal plane, almost like if it were a planet, and there was a sun around 1.5 units above the player.

monster hunter has a camera that functions like this too, though you can set the height of the camera, and the player isn't a sphere.

however, i think i can make your thing work, i'll try setting up an empty gameobject above the player and make the camera rotate around THAT ins$$anonymous$$d, i think this will work, i'll come back and confirm this if it works, for now though, it's getting late on my side of the planet.

avatar image sumeetkhobare · Feb 10, 2014 at 09:26 PM 0
Share

Yes, exactly as you said, Parent the player with some empty gameObject and then rotate the camera around it.. this way, the player can still roll and your camera wont do mad-eye rolls.. :)

PS: And I don't know why my code is co$$anonymous$$g in a single line. I write it in formatted fashion but it still comes in a single line, sometimes out of the code block.

avatar image SVGK · Feb 11, 2014 at 09:07 AM 0
Share

hrm, i've attempted it, and i failed, i also made an attempt to use the empty object as a parent for the camera and rotate it in the hopes that the camera would move and think it was still in the same spot, but no such luck.

i honestly have no idea what i'm supposed to do to overcome this line:

 transform.position = target.position + new Vector3(0, lift, distance);

this one line is the cause of my problems, turning the camera sort of works, it turns very slightly before being snapped back into position by this line.

i desperately need this, it's the very last thing i need for the game mechanics, and i can't get it to work.

avatar image sumeetkhobare · Feb 11, 2014 at 10:15 AM 0
Share

Okay.. Follow this, sorry i $$anonymous$$Now JS, don't know c# much.

RollScript.js

 function Update () {
      var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
       transform.Rotate(directionVector*5,Space.World);
   }



CameraRotateScript.js

 function Update () {
 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Q))
      transform.RotateAround(transform.position,Vector3.up,40*Time.deltaTime);
 else if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
     transform.RotateAround(transform.position,Vector3.up,40*-Time.deltaTime);
 }

Here's how my parent looks.

Here's how my Camera looks.

The Sphere.

The scene.

I used two planes just to create sloping, nothing else. see if this works good. Works good for me.

avatar image SVGK · Feb 11, 2014 at 12:13 PM 0
Share

this rotates the ball as well as the camera.

wait, i just thought of something, maybe i can have something like "cameraIsTurning == false" to make the camera set the position, i'm not confident about it, but it might just work.

Show more comments

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

19 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

Related Questions

Mouse orbit + Smooth follow script question 0 Answers

Control camera target from another scripts 1 Answer

Camera Control Help 0 Answers

character switching on collision 1 Answer

Why the control with gyroscope inverted when I make 180 degrees turn? 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