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 Bongmo · Feb 23, 2016 at 04:19 PM · c#rotationmovementaddforcecamera rotate

Roll a ball camera problem?

I use this official roll a ball code to move my ball: Roll a ball

I changed the above code with this following code, which I found: (EDIT: I found the link: Camera rotate)

 // ADDED -- This will keep track of the direction your camera is looking
 public Transform cam;

 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);
     */

     // 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);

 }


Everything works, except, if I move back. Then the camera rotates 360 degrees. Forward, left and right works. Only back is not working.

Why? I can't find the problem in the code.

I think, maybe there is a faster and better way to do this?

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
1

Answer by Magius96 · Feb 23, 2016 at 05:42 PM

I may be wrong, but what I'm getting from all this is that you are rolling a ball, and you want the camera to follow the ball. It sounds as if you have the camera attached to the ball and have to counter the rotations to prevent the camera from spinning.

Maybe it would be a better idea to NOT attach the camera to the ball, but instead write a new CameraFollow script and in its update method, set the camera's position relative to the ball's position.

     public class CameraFollow : MonoBehavior
     {
         [Tooltip("The object to follow.")]
         public GameObject FollowTarget;
         [Tooltip("The offset applied to the targets position for setting the cameras final location.")]
         public Vector3 PositionOffset;
     
         public void Update()
         {
             var pos = FollowTarget.transform.position + PositionOffset;
             transform.position = pos;
         }
     }

NOTE: I wrote the code from scratch so it may not work 100% right out of the gate.

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

106 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 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 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 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 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

Orbit Camera Around Player And Add Force 3 Answers

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

3D Heat Seeking Missile (C#) - Odd problem 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