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 /
  • Help Room /
avatar image
0
Question by boowee · Nov 03, 2016 at 11:39 AM · camera-movementcamera rotatecamera-look

Camera to follow one object and rotate towards another

Hi,

I am new to Unity and do not have much coding experience, so please bare with me. I am basically trying to do some cut-and-paste coding to achieve a small proof-of-concept where the camera is following one object (player1) but rotates based on the position of another (player2).

What I have now is a Main camera as a child to player1 which takes care of the following, but how can I rotate the camera around player1 based on the direction of where player2 is?

I have tried various things such as a lookAt target for the Main camera, or having the player1 and player2 always facing eachother, and even if the rotation works it always seems to override the following of player1.

You could think of this being a 3rd person game where player1 has an "auto aim" targeting player2. I.E player2 should always be straight to the left of player1 regardless of its position.

(right now I am just using two capsules on a plane)

Hope this makes some sense, otherwise I apologize.

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

1 Reply

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

Answer by Cynikal · Nov 03, 2016 at 11:51 AM

Since the camera is a child, it will follow player 1.

To have it look at another object, in your Update() put:

 transform.LookAt(Player2?);

The camera will face player 2 at all times.

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 Cynikal ♦ · Nov 03, 2016 at 11:53 AM 0
Share

You can also get the LookAtDirection, and add offsets.

avatar image boowee · Nov 03, 2016 at 12:21 PM 0
Share

Thank you for the response. I believe I tried this but that it did not then rotate around Player1, as it is actually should be looking at player 1, but from different angles based on the direction of player2.

Sorry if im confusing you. Will give this another go nonetheless

avatar image Cynikal ♦ boowee · Nov 03, 2016 at 12:27 PM 0
Share

Give me a little bit, i'll try to come up with a script for you.

avatar image Cynikal ♦ · Nov 03, 2016 at 12:51 PM 1
Share
 using UnityEngine;
 
 public class UnityAnswersHelp : $$anonymous$$onoBehaviour
 {
     public Transform EnemyPlayer;
     private Transform myCamera;
     public float CameraTurnSpeed = 5f;
 
     void Start()
     {
         myCamera = transform.FindChild("$$anonymous$$ain Camera");
     }
 
 
     private Vector3 _offset;
     void Update()
     {
         if (EnemyPlayer) //We have a EnemySelected.
         {
             Vector3 targetDir = EnemyPlayer.position - transform.position;
             float step = CameraTurnSpeed * Time.deltaTime;
             Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);
             Debug.DrawRay(transform.position, newDir, Color.red);
             transform.rotation = Quaternion.LookRotation(newDir);
         }
 
 
     }
 }
 

I tested it. Seems to work.

avatar image boowee Cynikal ♦ · Nov 03, 2016 at 02:26 PM 0
Share

Awesome, this worked perfectly. 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

79 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

Related Questions

viewing on the Mouse Y axis not working. 0 Answers

getting Jittery movement on camera when player rotating and moving in same time 0 Answers

CineMachine Camera Movement is inverse 1 Answer

(Solveed) How to make object face same direction as its parent? 0 Answers

Trying to Make a Smooth Camera Zoom While Changing Targets 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