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 C-Blunt · May 22, 2012 at 08:08 PM · c#androidcamerarotationfollow

Rotating camera problem C# Android

Greetings!

I'm having a problem whereby the camera keeps rotating when my spaceship rolls left/right as you turn left/right. Is there a way to lock a cameras rotatation to a set point or to only allow it to follow and not rotate...here is my script:

~EDIT~ Further info: The spaceship travels upwards along the Y axis and is controlled by the device's tilt function to move it left and right. I want the camera to follow the ship along the X & Y axis (2D movement only) and no rotation of the camera at all.

using UnityEngine; using System.Collections;

public class CameraFollow : MonoBehaviour {

 public Transform target;
 public float smooth = 0.3F;
 public float distance = 80.0F;
 private float yVelocity = 0.0F;
 
 void Update() {
     float yAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y, ref yVelocity, smooth);
     Vector3 position = target.position;
     position += Quaternion.Euler(0, yAngle, 0) * new Vector3(0, 0, -distance);
     transform.position = position;
     transform.LookAt(target);
 }

}

Thanks :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by rho21 · May 23, 2012 at 11:00 AM

If I understand you correctly, you want a camera that follows a fixed distance behind your spaceship, always points at the spaceship, and pitches and yaws with the spaceship, but doesn't roll with it.

How about something like (pseudo-code):

 //pitch adjusts x euler angle, yaw adjusts y euler angle, roll adjusts z euler angle
 //we need to undo the roll to get the desired up vector.
 Vector3 unrotatedUp = target.up rotated around target.forward by target.eulerAngles.z
 Vector3 cameraUp = //do some smoothing here between unrotatedUp and transform.up 
 transform.position = target.position + target.forward * -distance;
 transform.LookAt(target, cameraUp);
 

Obviously I've not tested any of this, so expect some bugs. :)

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 C-Blunt · May 23, 2012 at 06:54 PM 0
Share

Not as complicated as that :) it only needs to follow the ship upwards on the y axis, the distance and anglew should be the same but no rotation, i only want the ship to roll when manouvering. $$anonymous$$y game is a bit like doodle jump but with a spaceship ins$$anonymous$$d.

avatar image rho21 · May 24, 2012 at 11:20 AM 0
Share

O$$anonymous$$, so a 2D scrolling game.

Does one of your worldspace axes point out of the screen constantly, or does worldspace ever rotate? Does the camera need to rotate at all, or does it just slide along vertically, keeping pace with the ship?

In the simplest case, you never need to adjust the camera's rotation, and its position should just be (0, target.position.y, -distance) or similar.

If it's something a bit more complicated, you might want to try defining an up vector for the transform.LookAt in your code (though watch out for nasty effects when the target is along this vector from your camera).

avatar image
0

Answer by manimarancs · Mar 18, 2020 at 10:27 AM

Great blog !It is best institute.Top Training institute In Coimbatore http://glimtechnologies.com/aws-training-coimbatore/
http://glimtechnologies.com/angular-js-training-coimbatore/
http://glimtechnologies.com/dotnet-training-coimbatore/

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Smooth Follow Camera Rotate on Z-Axis? 2 Answers

Flip over an object (smooth transition) 3 Answers

How do I make a camera follow an object without changing its rotation in C#? 1 Answer

How to make an object face the mouse in a non-2D world 1 Answer

Problem with Camera rotation script 3 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