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 Zmiho · Apr 04, 2014 at 02:57 PM · cameramovement3rd person controller3rd person camera

Rotate 3rd person camera around player

How to rotate camera around player freely with joystick/ controller.

-Camera should collide with objects in scene. -If camera faces player's front part of the body, The player should pull left stick down to keep running forward. And so on.

So when you push right stick to the right, camera moves around player to the right side(orbiting).

  • When you push it to the left, camera orbits around the player to the left.

  • When you push stick up, camera zooms in (to specific length).

  • When you pull stick down towards yourself, camera zooms out (to specific length).

I was thinking maybe putting a sphere collider in which camera could move always facing the player. That could possibly make camera collide with objects in scene. But now, how to freely move the camera?

This is my script now: Camera:

 using UnityEngine;
 using System.Collections;
 
 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("Right Horizontal") * RotateSpeedPerTime;
         Pitch = Input.GetAxis("Right Vertical") * 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);
     }
 }

Movement:

 private var motor : CharacterMotor;
 
 // Use this for initialization
 function Awake () {
     motor = GetComponent(CharacterMotor);
 }
 
 // Update is called once per frame
 function Update () {
     // Get the input vector from kayboard or analog stick
     var directionVector = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
     
     if (directionVector != Vector3.zero) {
         // Get the length of the directon vector and then normalize it
         // Dividing by the length is cheaper than normalizing when we already have the length anyway
         var directionLength = directionVector.magnitude;
         directionVector = directionVector / directionLength;
         
         // Make sure the length is no bigger than 1
         directionLength = Mathf.Min(1, directionLength);
         
         // Make the input vector more sensitive towards the extremes and less sensitive in the middle
         // This makes it easier to control slow speeds when using analog sticks
         directionLength = directionLength * directionLength;
         
         // Multiply the normalized direction vector by the modified length
         directionVector = directionVector * directionLength;
     }
     
     // Apply the direction to the CharacterMotor
     motor.inputMoveDirection = transform.rotation * directionVector;
     motor.inputJump = Input.GetButton("Jump");
 }
 
 // Require a character controller to be attached to the same game object
 @script RequireComponent (CharacterMotor)
 @script AddComponentMenu ("Character/FPS Input Controller")
 
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 koray1396 · Apr 04, 2014 at 04:05 PM 0
Share

well, it's easy to rotate the camera around the player. to move the player in regards to the camera position, you can find the vector between the camera and the player, this vector will give you up and down movements, "normalized" vector will give right and left movement. so it's not very hard to figure out after that. camera to collide will not be very easy, you have to decide on a method. you can put colliders around the camera, and use rays at the same time. if the camera collides, say a wall, than the ray finds the distance between the wall and the player and either lifts up the camera or gets it near the player. maybe someone will give a better advice than me.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nazia08 · Apr 07, 2014 at 10:54 AM

 I know it's one of the commands that start with "thirdperson_", I don't know which one, though. 
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

23 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

Related Questions

How to make player move according to camera's rotation 0 Answers

How to stop 3rd person camera going throught walls ? 1 Answer

Camera collision while using accelerometer 1 Answer

MMO / WoW like 3rd person controller 2 Answers

How to move player in direction where the camera is aiming ? 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