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 Subwolferz · Sep 15, 2017 at 02:07 AM · cameralookatcamera movementthird-person-cameralockon

How to implement lock on with a camera that is not a child of the player object?

I'm trying to implement a lock-on system, so that when the player gets close to an enemy, the camera will focus on the enemy, while still keeping the player within view. Kind of like the Dark Souls lock on. Here is my script unaltered. Some of my attempts have been trying to use RotateAround or trying to use the vector between the player and the enemy as the position to use transform.LookAt() on. The object I'm trying to put focus on is the "enemy" GameObject.

 using UnityEngine;
 using System.Collections;
 
 public class ThirdPersonCamera : MonoBehaviour 
 {
     public Transform lookAt;
     public Transform startPos;
     enum cameraType {Inverse=-1, noInverse = 1};
 
     private const float Y_ANGLE_MIN = 15.0f;
     private const float Y_ANGLE_MAX = 50.0f;
     private float distance = 10.0f;
     private float distanceAbove = 5.0f;
     private float currentX = 0.0f;
     private float currentY = 0.0f;
     private float sensivityX = 4.0f;
     private float sensivityY = 1.0f;
     private float afkTimer = 0.0f;
     private float afkMaxTime = 10.0f;
     private int isInverse = (int)cameraType.noInverse;
     public GameObject enemy;
 
     private void Start()
     {
 
     }
 
     private void Update()
     {
         currentX += Input.GetAxis ("Mouse X") * sensivityX;
         currentY += Input.GetAxis ("Mouse Y") * isInverse * sensivityY;
 
         currentY = Mathf.Clamp (currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
     }
 
     private void LateUpdate()
     {
         if (Input.GetAxis("Mouse X") == 0 && Input.GetAxis("Mouse Y") == 0)
         {
             if (afkTimer >= afkMaxTime)
                 transform.position = Vector3.MoveTowards(transform.position, startPos.position, 0.1f);
             else
                 afkTimer += Time.deltaTime;
         }
         else
         {
             afkTimer = 0;
         }
 
         if (afkTimer < afkMaxTime)
         {
             Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
             Vector3 dir = new Vector3(0, 0, -distance);
             transform.position = Vector3.Slerp(transform.position, lookAt.position + rotation * dir, 0.08f);
         }
 
         transform.LookAt(lookAt.position + new Vector3(0, distanceAbove, 0));
     }
 }
 

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

Answer by DidaFM · Apr 19, 2019 at 05:21 PM

Did you manage to make this work? I have the same question and would like to find the answer.

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 Subwolferz · Apr 19, 2019 at 05:47 PM 0
Share

I toyed with it a bit but never really got to an answer. However, I have a friend who I think knows how to do this. I'm going to ask him and get back to you.

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

[SOLVED] Third Person Spherically Orbiting Camera 1 Answer

Third Person Camera 1 Answer

Player rotates with camera(face same direction as the camera) 1 Answer

Camera is moving strangely 2 Answers

How can i make my camera rotate/move relative to my players rotation? 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