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 GamingTopTen · May 17, 2018 at 05:49 AM · cameracamera-movementcamera-lookcamera follow

Resetting Camera with a key input

Hello,

This problem has been driving me to drink. When I hit "m", the camera faces at the right angle of (35,0,0), BUT it refuses to move back to the original position of "behind" the character. It will stay at its current position no matter what!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MyOrbit : MonoBehaviour
 {
     public Transform target;
     public float sensitivity = 3.0f;
     public Camera Camera;
     public Vector3 defaultCameraPosition = new Vector3(0f, 10f, -10f);
 
     private Vector3 offset;
 
     void Start()
     {
         offset = (transform.position - target.position).normalized * 10;
         transform.position = target.position + offset;
         GetComponent<Camera>();
     }
 
     void Update()
     {
         Quaternion q = Quaternion.AngleAxis(Input.GetAxis("Horizontal (Camera)") * sensitivity, Vector3.up);
         offset = q * offset;
         if (Input.GetKeyDown("m"))
         {
             transform.rotation = Quaternion.Euler(35, 0, 0);
             Camera.main.transform.localPosition = Vector3.Lerp(Camera.main.transform.localPosition, defaultCameraPosition, 0);
         }
         transform.rotation = q * transform.rotation;
         Camera.main.transform.position = target.position + offset;
     }
 }

I used a MyOrbit script found elsewhere on answers, and it has been helpful getting the darn thing to rotate around the character, but that's where the helpfulness ends. I just want to be able to reset the camera position when I hit m, as if I never touched it.

Help me before I turn to coping vices.

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 KaspianR · May 17, 2018 at 07:11 AM

This might work:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class MyOrbit : MonoBehaviour
  {
      public Transform target;
      public float sensitivity = 3.0f;
      public Camera cam;
      public Vector3 defaultCameraPosition = new Vector3(0f, 10f, -10f);
  
      private Vector3 offset;
  
      void Start()
      {
          offset = (transform.position - target.position).normalized * 10;
          transform.position = target.position + offset;
          cam = GetComponent<Camera>();
      }
  
      void Update()
      {
          Quaternion q = Quaternion.AngleAxis(Input.GetAxis("Horizontal (Camera)") * sensitivity, Vector3.up);
          offset = q * offset;
          if (Input.GetKeyDown("m"))
          {
              transform.rotation = Quaternion.Euler(35, 0, 0);
              cam.transform.localPosition = Vector3.Lerp(cam.transform.localPosition, defaultCameraPosition, 0);
          }
          transform.rotation = q * transform.rotation;
          cam.transform.position = target.position + offset;
      }
  }


Remember to never name a variable something that is already a name in Unity, Camera is a variable that stores a Camera component so I renamed the variable to cam instead. But just let me know if that didn't fix it and I'll take another look at your script. Good luck!

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 GamingTopTen · Jun 12, 2018 at 03:24 AM 0
Share

Hey, sorry for sleeping on this for a month. This doesn't work. I believe the issue lies in line number 28; I removed that entire line and it runs just the same. when I hit "m".

Thanks for trying to help, though, and let me know if you have any new ideas, because I exhausted all of $$anonymous$$e.

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

141 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 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

Returning the camera back to original position, 1 Answer

Camera smooth follow behaviour 0 Answers

Camera View Problem 0 Answers

When I lock my camera to my player I saw only my default backround please help me :( 2 Answers

Make camera follow player and align direction 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