• 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 OctoSloths · Jul 05, 2015 at 11:18 PM · c#camerarotationtransformrotate

Moving Camera on player stop then move and rotate to separate point?

Currently I have a player that moves at a constant rate with the camera as a child object. I want to have it so that when I click a certain target object, the camera moves and is positioned to face the targeted object. Right now it moves to the specified position in front of the target when clicked, but does not rotate to face the target. Instead the camera begins spinning or faces a random incorrect direction. Here is my current script and thank you for all help:

 using UnityEngine;
 using System.Collections;
 
 public class CameraSwitch : MonoBehaviour
 {
 
     //Handling
     public Transform main;
    // public Transform select;
    public Transform cam;
     public GameObject about;
     //public Transform survival;
     //Internal
     Transform goal;
     float countdown = 5;
     bool onHuman = false;
      GameObject controller;
      public Transform target;
     void Start()
     {
         //cam = GameObject.FindWithTag("MainCamera").transform;
        // main = GameObject.FindWithTag("Main").transform;
         controller = GameObject.FindWithTag("Control");
         //about = GetComponentInChildren<GameObject>().transform;
         Time.timeScale = 1.0f;
         goal = main;
     }
 
     public void GoToMain()
     {
         goal = main;
     }
 
     public void GoToAbout()
     {
         goal = about.transform;
     }
 
     /*public void GoToSelectCampaign()
     {
         goal = select;
     }
     public void GoToSelectSurvival()
     {
         goal = survival;
     }*/
     void Raycast()
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
          if (Physics.Raycast(ray, out hit))
          {
              bool isHit;
              isHit = false;
                 
          }
         if (hit.collider != null && hit.collider.tag == "Human")
         {
             onHuman = true;
             //cam.transform.LookAt(target);
             cam.eulerAngles = Vector3.MoveTowards(cam.eulerAngles, goal.eulerAngles, Time.deltaTime * 500);
             print("CLICCKKKK");
             //if (hit.collider.tag == "Human")
             // {
             print("click");
             GoToAbout();
             
             Debug.Log("Target Position: " + hit.collider.gameObject.transform.position);
             //}
 
         }
     }
 
     void Update()
     {
         cam.position = Vector3.MoveTowards(cam.position, goal.position, Time.deltaTime * 100f);
        // print(goal.eulerAngles)
         //cam.eulerAngles 
         //cam.transform.Rotate(24,180,0);
        // cam.eulerAngles = Vector3.Slerp(cam.eulerAngles, goal.eulerAngles, Time.deltaTime * 500);
         if (Input.GetMouseButtonDown(0))
         {
             Raycast();
             
             
            
         }
         if (onHuman)
         {
             controller.GetComponent<FirstPersonController>().enabled = false;
             countdown -= Time.deltaTime;
             print(countdown);
             if (countdown <= 0)
             {
                 Debug.Log("main");
                 GoToMain();
                 controller.GetComponent<FirstPersonController>().enabled = true;
                 countdown = 5;
                 onHuman = false;
                 
             }
         }
 
             
 
     }
 
 }
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Turning character with transform.Rotate 0 Answers

How i can equal 2 Tranform Rotations? 1 Answer

transform.Rotate will not work 1 Answer

Movement with camera and rotation of objects 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