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 Rob 6 · Jul 09, 2011 at 10:55 AM · camerarotationaxisfixed

Camera rotation around a single axis - following a rolling ball

I'm sure this is very simple, there's just one line I can't quite work out! I have an empty object at the centre of a controllable ball. The camera is a child of this empty object. This object needs to stay with the ball, and only rotate around the y axis, so that the camera swings round when you turn the ball. So far I have only managed to get as far as having the camera follow the ball with no inherited rotation whatsoever. I just can't find a way to split the ball's rotational Quaternion into a Vector3 and then carry that Vector3's y value to a new Vector3 to be converted back into a Quaternion for the camera's parent.

Here's what I've got so far - this script is attached to the empty gameobject at the centre of the ball:

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour 
 {
     public Transform ballObject;

     void Update() 
     {
         transform.position = ballObject.transform.position;
         transform.rotation = Quaternion.Euler(0, ballObject.transform.rotation.y, 0);
     }
 }

There are no compiler errors, yet it just doesn't do anything! I'm currently getting used to C# after using JS for all my previous Unity projects, so I'm assuming there's just something I've missed!

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
1
Best Answer

Answer by Rob 6 · Jul 10, 2011 at 02:33 PM

I've created a slightly more workable solution, merging the movement and camera scripts together. Now, the "ball centre" object that the camera is attached to controls turning, while forward and backwards controls apply a force on the ball in the direction that the camera is facing.

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour 
 {
     public Transform ballObject, ballCamera;
     public float speed, turnSpeed;
     Vector3 forceDirection;

     void FixedUpdate() 
     {
         transform.position = ballObject.position;
         if(Input.GetAxis("Vertical") != 0)
         {
             RollBall();
         }
         //calculate ball turning
         transform.Rotate((Vector3.up * Input.GetAxis("Horizontal") * turnSpeed), Space.World);
     }
 
     void RollBall()
     {
         //set force down camera's look direction
         forceDirection = ballCamera.transform.forward;
 
         //remove y force direction for angled camera
         forceDirection = new Vector3(forceDirection.x, 0, forceDirection.z);
 
         //add force to ball
         ballObject.rigidbody.AddForce(forceDirection.normalized * speed * (Input.GetAxis("Vertical")));
     }
 }

The result is that the movement can be considered in a way that is independent of the rotation of the ball - so it's more of a work-around than a solution, but there didn't seem to be a graceful way to clone a rotation value across via a Vector3.

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 Ludiares.du · Jul 09, 2011 at 11:17 AM

Try this:

using UnityEngine; using System.Collections;

public class CameraScript : MonoBehaviour { public Transform ballObject;

 void Update() 
 {
     transform.position = ballObject.position;
     transform.rotation.eulerAngles = new Vector3(0, ballObject.transform.rotation.y, 0);
 }

}

You don't need to add the .transform after ballObject because it is a transform.

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 Rob 6 · Jul 10, 2011 at 09:51 AM 0
Share

Thanks, but that line causes it to throw an error:

"error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.rotation'. Consider storing the value in a temporary variable"

avatar image aldonaletto · Jul 10, 2011 at 01:42 PM 0
Share

What exactly are you trying to do? $$anonymous$$ake the camera face always the direction of movement?

avatar image
0

Answer by aldonaletto · Jul 10, 2011 at 02:13 PM

If what you want is just to keep the camera facing always the direction of movement, you must calculate its direction based on the last position. To avoid inaccuracies produced by very small displacements, you can set a minimum distance to refresh the view direction (adjust the distance variable in the Inspector if needed):

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour 
 {
     public Transform ballObject;    
     public float distance = 0.1f; // min distance to refresh view direction
 
     Vector3 lastPos;
     Vector3 viewDir;
     
     void Start(){
         // ensure forward direction at start
         lastPos = ballObject.transform.position - transform.forward * distance; 
     }        
     
     void Update() 
     {
         Vector3 ballPos = ballObject.transform.position;
         Vector3 newDir = ballPos - lastPos;  // direction from last position
         newDir.y = 0;    // keep the camera on horizontal plane
         if (newDir.magnitude>distance){  // only recalculate after min distance
             viewDir = newDir;
             lastPos = ballPos;
         }
         transform.position = ballPos;
         transform.forward = Vector3.Slerp(transform.forward, viewDir.normalized, Time.deltaTime);
     }
 }
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 HaykAv · Oct 06, 2018 at 05:28 PM 0
Share

Your answers are always on point! 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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Lock an axis from rotating 1 Answer

fixing rotation on a specific axis 1 Answer

Player moving/rotating along a single axis 1 Answer

Rotate according to the camera on Y and Z only! 2 Answers

How to rotate an object on one axis facing another object? 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