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 Minaego · Nov 18, 2014 at 03:55 PM · camerafolloworbitturretcombine

Combining two camera modes

I've been experimenting with a vehicle-based game where you can drive and fire a turret. I'd like the camera to follow the vehicle while turning, but also have it orbit around the vehicle to aim the turret at the same time.

These two (simplified) scripts work fine independently but trying to combine them is giving me an headache.

My follow script:

 float currentAngle = transform.eulerAngles.y;
 float desiredAngle = target.transform.eulerAngles.y;
 float angle = Mathf.LerpAngle(currentAngle, desiredAngle, Time.deltaTime);
          
 var rotation = Quaternion.Euler(0, angle, 0);
 transform.position = target.transform.position - (rotation * offset);
          
 transform.LookAt(target.transform);


My mouse orbit script:

 x += Input.GetAxis("Mouse X") * Time.deltaTime;
 y -= Input.GetAxis("Mouse Y") * Time.deltaTime;
 
 var rotation = Quaternion.Euler(y, x, 0f);
 
 var position = rotation * target.position;
 transform.rotation = rotation;
 transform.position = position + new Vector3(0f, 2.4f, 0f);

Comment
Add comment · Show 2
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 Minaego · Nov 18, 2014 at 06:57 PM 0
Share

The one part I forgot to explain is that I want to have the camera rotate when the vehicle turns as well. So that it rotates when turning the car and when turning the turret.

Thank you for the effort though

avatar image Scribe · Nov 19, 2014 at 12:11 PM 0
Share

oh that's even easier then, make the turret a child of the vehicle, and then attach the mouse look script from the character controller standard assets package to the turret object!

1 Reply

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

Answer by Scribe · Nov 18, 2014 at 05:17 PM

Here's a script I have used previously, maybe it would be useful to you!

Note that the code currently in FixedUpdate was just for testing purposes to see if this was the correct code, it is not advisable to set the velocity of a rigidbody directly and there are better choices for controlling rotation than is used here as well!

 public GameObject target;
 Rigidbody targetRB;
 Transform targetTrans;
 Transform thisTrans;
 public Vector3 orbitOffset = new Vector3(0, 2, 0);
 float viewAngle = 0;
 float rot = 0;
 public float rotationSpeed = 360;
 public float zoomSpeed = 10;
 float distance;
 public float minDist = 5;
 public float maxDist = 10;
 public float maxSpeed = 10;
 Vector3 lookPos;
 Quaternion origRot;
 
 void Start(){
     targetRB = target.rigidbody;
     targetTrans = target.transform;
     distance = Mathf.Clamp(distance, minDist, maxDist);
     thisTrans = transform;
     lookPos = targetTrans.position + orbitOffset;
     origRot = thisTrans.rotation;
 }
 
 void Update(){
     lookPos = targetTrans.position + orbitOffset;
     if(targetRB != null){
         distance = Mathf.Lerp(minDist, maxDist, targetRB.velocity.sqrMagnitude/(maxSpeed*maxSpeed));
         distance = Mathf.Clamp(distance, minDist, maxDist);
     }
     
     viewAngle += Input.GetAxis("Mouse Y")*rotationSpeed*Time.deltaTime;
     rot += Input.GetAxis("Mouse X")*rotationSpeed*Time.deltaTime;
     viewAngle = Mathf.Clamp(viewAngle, -80, 10);
     
     Quaternion xQuaternion = Quaternion.AngleAxis (rot, Vector3.up);
     Quaternion yQuaternion = Quaternion.AngleAxis (viewAngle, Vector3.left);
     thisTrans.rotation = origRot * xQuaternion * yQuaternion;
     thisTrans.position = lookPos - (thisTrans.forward*distance*Mathf.Lerp(0.5f, 1, (10-viewAngle)/90));
 }
 
 void FixedUpdate(){
     //REPLACE THIS WITH BETTER MOVEMENT CODE
     if(targetRB == null){
         return;
     }
     Vector3 v = targetRB.velocity;
     v = Input.GetAxis("Vertical")*maxSpeed*targetTrans.forward;
     targetRB.velocity = v;
     targetTrans.Rotate(Vector3.up, Input.GetAxis("Horizontal")*rotationSpeed/2*Time.deltaTime);
 }

Scribe

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

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 position not updating 1 Answer

Mouse Orbit + Move Object + Follow Problem 1 Answer

Super Monkey Ball type Camera? 2 Answers

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

How to make your character move? 7 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