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 PixelProtist · Jul 03, 2014 at 12:47 PM · cameracamera-movementdirectionplatformerthird-person

Moving 3rd person player relative to orbiting camera

Hello all, I am working on a prototype for a 3D platformer. :) I have set up some scripts, but I have encountered a big problem. If you ever played Mario 64 or any of the 3D Zeldas, you will know that they work in a strange way. When you push your joystick up or down, the player moves away from or towards the camera, and when you push your joystick left or right, he actually orbits around the camera, because the camera changes angle to face him and he continues to move to the camera's right or left. It is also important to note that the directions can be mixed, so if you push to the upper right, he actually spirals outwards away from the camera, as he moves both away from the camera and around it. My player is currently a simple cube with a character controller and this script, which is designed to work for a controller although it also works with a keyboard. He moves around using wasd or a joystick, but he moves relative to the world axis, not the camera axis. Obviously this causes problems, because if the camera is rotated through 180 degrees, all the controls are inversed!

 var horizontalSpeed = 1.0f;
 var verticalSpeed = 1.0f;
  
 var rotationSpeed = 60.0f;
  
 var jumpSpeed = 8.0f;
 var gravity = 14.0f;
 
 var cameraTransform : Transform;
 var axis : Vector3 = Vector3.up;
 
  
 private var moveDirection = Vector3.zero;
 private var controller : CharacterController;
 
  
 function Update()
 {
     controller = gameObject.GetComponent(CharacterController);

 
 if (controller.isGrounded)
 {
 
     var horizontalDirection = Input.GetAxis("Horizontal") * horizontalSpeed;
     var forwardDirection = Input.GetAxis("Vertical") * verticalSpeed;
     moveDirection = new Vector3(horizontalDirection, 0, forwardDirection);
 
 
     if (Input.GetButton("Jump"))
     {
         moveDirection.y = jumpSpeed;
     }
 }
 else
 {
     horizontalDirection = Input.GetAxis("Horizontal") * horizontalSpeed / 2;
     forwardDirection = Input.GetAxis("Vertical") * verticalSpeed / 2;
     moveDirection = new Vector3(horizontalDirection, moveDirection.y, forwardDirection);
 
     moveDirection.y -= gravity * Time.deltaTime;
 }
 
 if (moveDirection.magnitude > 0.05)
 {
     transform.LookAt(transform.position + new Vector3(moveDirection.x, 0, moveDirection.z));
 }
 controller.Move(moveDirection * Time.deltaTime);
 }




Then there's the camera, which has 2 scripts.

The standard one to follow the player's movement:

 var target : Transform;
 
 var rotationDamping = 3.0;
 
 @script AddComponentMenu("Camera-Control/Smooth Follow")
 
 function LateUpdate () {
 
     if (!target)
         return;
     
     var wantedRotationAngle = target.eulerAngles.y;
         
     var currentRotationAngle = transform.eulerAngles.y;
     
     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 
     var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
     
     transform.LookAt (target);
 }



And another one, which keeps it the right distance from the player and orbits around him:

 var setHeight : float;
 var maxDistance : float;
 var minDistance : float;
 var target : Transform;
 var axis : Vector3 = Vector3.up;
 var rotationSpeed = 100;
 
 function Update ()
 {
     var distance = Vector3.Distance(this.transform.position, target.transform.position);
 
 if(distance > maxDistance)
 {
     Debug.Log("Too far away");
     //Vector3.MoveTowards(transform.position, target.transform.position, 100);
     //transform.position += Vector3.forward * 4 * Time.deltaTime;
     transform.Translate(Vector3.forward * 10 * Time.deltaTime);

 }
 else if(distance < minDistance)
 {
     Debug.Log("Too close");
     transform.Translate(Vector3.back * 10 * Time.deltaTime);
 }
 
 transform.position.y = target.position.y + setHeight;

 
 if(Input.GetAxis("RightHorizontal") < 0)
 {
     transform.RotateAround (target.position, axis, rotationSpeed * Time.deltaTime);
 }
 else if(Input.GetAxis("RightHorizontal") > 0)
 {
     transform.RotateAround (target.position, axis, -rotationSpeed * Time.deltaTime);
 }    
 }

I would very much appreciate any help with this, either with the existing code or new code entirely. It needn't be particularly efficient, as this is just a prototype, and C# alternatives are also fine! :) Thanks! :D

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 yanuaris · Feb 29, 2016 at 12:06 PM

@PixelProtist

I'm still solving this too myself, and i'm not near any unity to proof this. Assuming you've made your variable to contain the camera transform...

 moveDirection = new Vector3(horizontalDirection, 0, forwardDirection);

We can start from here, where you should probably let moveDirection acquire Vector(cameratransform.right,0, cameratransform.forward);

This one can also be your solution, calling LookAt first to make the character point towards that specific direction from the camera angle.

  if (moveDirection.magnitude > 0.05)
  {
      transform.LookAt(transform.position + new Vector3(0, 0, cameratransform.forward));
  }

While this one feels more direct, applied to your character.

 transform.LookAt(cameratransform.forward);

I'm nowhere close to being sure and seeing how you type so much i think you'll understand more if you test it. Let me know about the result and maybe once i got back from work i'll try your code and see if i can solve it.

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 PixelProtist · Mar 06, 2016 at 07:53 PM 0
Share

@yanuaris That's very interesting and surprisingly simple... I'll try this out when I next get chance. I haven't done any program$$anonymous$$g for a while, but hopefully we'll work this out eventually! Thanks

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Camera movement in a Third Person Shooter Game 2 Answers

I only rotated X, but Y and Z are shifting as well when I play the game. 1 Answer

How can I mimic this game's camera in Unity? 1 Answer

Stop camera from going through walls 2 Answers

Third person camera aim 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