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 AceGamesCo · Nov 13, 2015 at 01:47 PM · c#rotationmovementaddforcecamera rotate

Orbit Camera Around Player And Add Force

I'm a beginner to unity, but I am liking what I have seen so far. I've started working on a project based off of the Roll-A-Ball tutorial in the Learn section, but I'm stuck! alt text alt text I want the camera to rotate around the ball so players can get a better view of the area, but at the same time, have the ball move in the same direction as the camera. Right now I have a simple script the rotates the camera around, but when I press the up arrow key to move forward the ball looks like it's rolling sideways. I have been trying to make a C# script that uses addForce to move it in the right direction, but have failed miserably.

I'm sorry if i'm not being specific enough, and would really be very grateful for any help

CameraRotate↓

 using UnityEngine;
 using System.Collections;
 
 public class CameraRotate : MonoBehaviour{
     
     public Transform target;
     public float distance = 10.0f;
     public float sensitivity = 3.0f;
     
     private Vector3 offset; 
     
     void Start ()  {
         offset = (transform.position - target.position).normalized * distance;
         transform.position = target.position + offset;
     }
     
     void Update () {
         Quaternion q = Quaternion.AngleAxis(Input.GetAxis ("Mouse X") * sensitivity, Vector3.up);
         offset = q * offset;
         transform.rotation = q * transform.rotation;
         transform.position = target.position + offset;
     }
 }

PlayerController↓

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     
     public float speed;
     
     private Rigidbody rb;
     
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
     }
     
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         
         rb.AddForce (movement * speed);
     }
 }


cool.jpg (53.4 kB)
untitled.png (65.4 kB)
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

3 Replies

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

Answer by Eno-Khaon · Nov 14, 2015 at 06:05 AM

At the moment, your "movement" Vector3 is strictly defined in world coordinates. If you want your control scheme to change with your camera's perspective, you're going to need to take the camera itself into account.

Therefore, in your PlayerController script, you'll want to make a few adaptations:

 public float speed;
 
 // ADDED -- This will keep track of the direction your camera is looking
 public Transform cam;
 
 private Rigidbody rb;
 
 // ...
 
 // CHANGED -- This limits movement speed so you won't move faster when holding a diagonal. It's just a pet peeve of mine
 Vector2 inputDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
 if(inputDirection.sqrMagnitude > 1)
 {
     inputDirection = inputDirection.normalized;
 }
 
 // CHANGED -- This takes the camera's facing into account and flattens the controls to a 2-D plane
 Vector3 newRight = Vector3.Cross(Vector3.up, cam.forward);
 Vector3 newForward = Vector3.Cross(newRight, Vector3.up);
 Vector3 movement = (newRight * inputDirection.x) + (newForward * inputDirection.y);
 
 rb.AddForce(movement * speed);

Okay, now let's break down the changes I made:

First, I added the camera's transform to the script. It should be a simple drag-and-drop.

Second, I put a limit on the input. As I described in the commented script, it's just my personal preference there.

Most importantly, third, I rebuilt a few axes. I created a guaranteed right-facing vector by calculating the vector perpendicular to the camera's forward vector and the world's up vector. While this would almost definitely result in the same vector as "cam.right" would, I use it to account for the possibility that the camera itself leans. This flattens the angle to a (near-) guaranteed "right" from the camera, relative to the world.

Then, I do the same for the forward vector. The "right" vector is now defined, so I make use of that to calculate a new forward, flat against the ground. The camera's pitch is no longer relevant and will not be a factor in moving the ball.

Finally, I implement the new right and forward directions by multiplying them by the two-dimensional inputs. the horizontal axis input (x) is multiplied by the new "right" and the vertical axis input (y) is multiplied by the new "forward" to apply that input to those specific axes.

Comment
Add comment · Show 2 · 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 patil · Feb 08, 2016 at 12:38 PM 0
Share

thank you so much!

avatar image SoshJam · Aug 07, 2017 at 04:13 PM 0
Share

This worked great. I was just wondering how I could make it so you could change the Y of the camera as well.

avatar image
0

Answer by Bassel0 · Nov 09, 2016 at 08:30 AM

I can't drag the camera to the player even though I defined transform called camera that's why the player cannot build its new movement axes depending on the camera

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 SoshJam · Aug 10, 2017 at 03:14 PM

Is there a way to stop the camera from going through walls with this script, and/or make it so you can control the camera's Y-axis? @Eno-Khaon

Comment
Add comment · Show 5 · 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 Eno-Khaon · Aug 13, 2017 at 10:03 AM 0
Share

$$anonymous$$oving the camera up and down can be tricky to fine-tune, but should be reasonably simple to implement. Where the camera is set to rotate on the X-axis using...

 Quaternion.AngleAxis(mouse$$anonymous$$ovementX, Vector3.up)

... the camera can be tilted up and down using something like...

 Quaternion.AngleAxis(mouse$$anonymous$$ovementY, Vector3.right)

... and then concatenated with the previous rotation through an additional multiplication.


$$anonymous$$eeping the camera from passing through walls, however, adds a fair bit of complexity and goes outside the scope of the original question.

I would recommend checking for questions specifically on that topic (and ask about it yourself if you can't find what you're looking for).

avatar image SoshJam Eno-Khaon · Aug 14, 2017 at 03:05 PM 0
Share

Thanks! This was great.

avatar image SoshJam Eno-Khaon · Aug 14, 2017 at 03:18 PM 0
Share

Should the inside of the update method look like this then?

 Quaternion q = Quaternion.AngleAxis(Input.GetAxis("$$anonymous$$ouse X") * sensitivity, Vector3.up);
     Quaternion r = Quaternion.AngleAxis(Input.GetAxis("$$anonymous$$ouse Y") * sensitivity, Vector3.right);
     offset = q * offset;
     transform.rotation = q * r * transform.rotation;
     transform.position = target.position + offset;

avatar image Eno-Khaon SoshJam · Aug 15, 2017 at 08:16 PM 0
Share

There are a lot of different ways to handle camera control, so there's not exactly any specific way for it to work.

For example, it would really just be a matter of opinion whether the Y-rotation should also be factored into the camera's offset position.

 // something like...
 offset = q * r * offset;

As far as what would work best, however... well, there's not really a best to speak of, so there's no perfect solution to offer anyway.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Roll a ball camera problem? 1 Answer

Flip over an object (smooth transition) 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

How to make the player rotate to where an object is being thrown? 1 Answer

GetKeyUp not registering 2 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