Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 tvwxyz · Jan 26, 2014 at 09:14 PM · joystickorbit3rd person camerajoysticksdual

Dual-Joystick Controlled 3rd-Person Orbit Camera

I am brand-new to Unity. :)

I am trying to make a dual-joystick controlled 3rd-person "orbit" camera (arcball camera? / trackball camera?). The camera follows the player and can rotate around the player in a sphere or bubble centered on the player. This is the type of camera used in games like Dark Souls and Naughty Bear. The left joystick moves and rotates the player, and the right joystick rotates the camera.

Dark Souls' camera is the best third-person camera I have ever used and is the ideal I eventually want: it is "intelligent", automatically reorients itself after a few seconds, and never gets obscured by walls or other objects. However, I know that it is probably quite complex, even in Unity. On the other hand, Naughty Bear's camera is quite adequate and much simpler to implement. It doesn't reorient itself and can get obscured but it follows the player exactly the way I want to. For now, I'm quite satisfied just getting a Naughty Bear camera to work! :)

I searched for awhile but never found quite what I wanted. I looked through some of the demo projects' code and saw some good cameras but I'm not familiar enough with Unity yet to extract only the parts I need without having to undo lots of other stuff.

The code below is as simple as I could make it. It is working fairly well but isn't quite what I want and I'm sure it can be improved. At this point, I'm not really concerned about player movement but just want to get the camera working like I want. Note that I've mapped my joystick axes to LeftX, LeftY, RightX and RightY. I've also set the Player as the target of the Camera.

Thanks for any insights! :)

 public class PlayerScript : MonoBehaviour
 {
     public float RotateSpeed = 150,
         MoveSpeed = 50;
     float DeltaTime;
 
     void Update()
     {
         DeltaTime = Time.deltaTime;
         transform.Rotate(0, Input.GetAxis("LeftX") * RotateSpeed * DeltaTime, 0);
         transform.Translate(0, 0, -Input.GetAxis("LeftY") * MoveSpeed * DeltaTime);
     }
 }
 
 public class CameraScript : MonoBehaviour
 {
     public GameObject Target;
     public float RotateSpeed = 170,
         FollowDistance = 20,
         FollowHeight = 10;
     float RotateSpeedPerTime,
         DesiredRotationAngle,
         DesiredHeight,
         CurrentRotationAngle,
         CurrentHeight,
         Yaw,
         Pitch;
     Quaternion CurrentRotation;
 
     void LateUpdate()
     {
         RotateSpeedPerTime = RotateSpeed * Time.deltaTime;
 
         DesiredRotationAngle = Target.transform.eulerAngles.y;
         DesiredHeight = Target.transform.position.y + FollowHeight;
         CurrentRotationAngle = transform.eulerAngles.y;
         CurrentHeight = transform.position.y;
 
         CurrentRotationAngle = Mathf.LerpAngle(CurrentRotationAngle, DesiredRotationAngle, 0);
         CurrentHeight = Mathf.Lerp(CurrentHeight, DesiredHeight, 0);
 
         CurrentRotation = Quaternion.Euler(0, CurrentRotationAngle, 0);
         transform.position = Target.transform.position;
         transform.position -= CurrentRotation * Vector3.forward * FollowDistance;
         transform.position = new Vector3(transform.position.x, CurrentHeight, transform.position.z);
 
         Yaw = Input.GetAxis("RightX") * RotateSpeedPerTime;
         Pitch = Input.GetAxis("RightY") * RotateSpeedPerTime;
         transform.Translate(new Vector3(Yaw, -Pitch, 0));
         transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
 
         transform.LookAt(Target.transform);
     }
 }
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 dotfortun3 · Jan 26, 2014 at 10:40 PM

Below is the code I used currently for my camera, minus the collision handling:

 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
 
 
 y += Input.GetAxis ("VerticalJoyR") * ySpeed * 0.02f;
 x += Input.GetAxis ("HorizontalJoyR") * xSpeed * 0.02f;
 
 y = ClampAngle(y, yMinLimit, yMaxLimit);
 Quaternion rotation = Quaternion.Euler(y, x, 0f);
 transform.rotation = rotation;
 HandleCollision();
 Vector3 position = rotation * new Vector3(currXOffset, height, -_currDistance) + target.position;
 _standardPosition = rotation * new Vector3(currXOffset, height, -distance) + target.position;
                 
 transform.position = position;

If you modified it like this, it may work for what you need:

 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
 
 
 y += Input.GetAxis ("RightY") *  RotateSpeedPerTime;
 x += Input.GetAxis ("RightX") *  RotateSpeedPerTime;
 
 y = ClampAngle(y, yMinLimit, yMaxLimit);
 Quaternion rotation = Quaternion.Euler(y, x, 0f);
 transform.rotation = rotation;
 Vector3 position = rotation * new Vector3(0f, height, FollowDistance) + target.position;
                 
 transform.position = position;
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

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

Orbiting Bullets 1 Answer

Joystick moves unexpectedly in Unity 1 Answer

Dual Joysticks in First Person Controller 1 Answer

How to set horizon level for camera orbit script? 0 Answers

Input Axis names and axis type (help with joysticks) 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