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 awsome129 · Mar 13, 2017 at 08:11 PM · camerarotationrotatesnapmain menu

Help with camera snap

So in my camera script I added a section where if the player pressed numpad 5, the camera will snap to a greater y axis, and on a 95 degree rotation. The problem is, it won't work and I don't know why. Please Help! P.S. Please excuse any bad math on my part in my script section, I did this late at night, but It would help if u could point it out if there is any.

Section of script which is dealing with this : Quaternion CameraRotSnap;

 void Start()
 {
     MovableObjScript = GameObject.Find("MoveObjectsController").GetComponent<PlayerMovableObjectsManager>();
 }

 void Update()
 {

     float CameraPosSnapX = Camera.main.transform.position.x;
     float CameraPosSnapY = Camera.main.transform.position.y;
     float CameraPosSnapZ = Camera.main.transform.position.z;
     float CameraRotSnapX = Camera.main.transform.rotation.x;
     float CameraRotSnapZ = Camera.main.transform.rotation.z;

     if (Input.GetButtonDown("[5]"))
     {
         CameraPosSnapX = transform.position.x;
         CameraPosSnapY = transform.position.y + 10;
         CameraPosSnapZ = transform.position.z;
         CameraRotSnap =  Quaternion.Euler(CameraRotSnapX, 95, CameraRotSnapZ);
     }

Rest of script in case of conflict: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CameraController : MonoBehaviour {

 public float dragSpeed = 1f;
 private Vector3 dragOrigin;
 float MouseRotateSpeed = 3;
 float SelectionBoxWidth;
 float SelectionBoxHeight;
 float SelectionBoxTop;
 float SelectionBoxLeft;
 Vector2 SelectionBoxStart;
 Vector2 SelectionBoxFinish;
 Vector3 SelectionBoxMouseDownPoint;
 Vector3 SelectionBoxCurrentMousePos;
 private PlayerMovableObjectsManager MovableObjScript;
 Quaternion CameraRotSnap;

 void Start()
 {
     MovableObjScript = GameObject.Find("MoveObjectsController").GetComponent<PlayerMovableObjectsManager>();
 }

 void Update()
 {

     float CameraPosSnapX = Camera.main.transform.position.x;
     float CameraPosSnapY = Camera.main.transform.position.y;
     float CameraPosSnapZ = Camera.main.transform.position.z;
     float CameraRotSnapX = Camera.main.transform.rotation.x;
     float CameraRotSnapZ = Camera.main.transform.rotation.z;

     if (Input.GetButtonDown("[5]"))
     {
         CameraPosSnapX = transform.position.x;
         CameraPosSnapY = transform.position.y + 10;
         CameraPosSnapZ = transform.position.z;
         CameraRotSnap =  Quaternion.Euler(CameraRotSnapX, 95, CameraRotSnapZ);
     }
      
     SelectionBoxCurrentMousePos = Input.mousePosition;

     if (Input.GetMouseButtonDown(1))
     {
         SelectionBoxMouseDownPoint = Input.mousePosition;
     }

     //GUI Vars
     SelectionBoxWidth = Camera.main.WorldToScreenPoint(SelectionBoxMouseDownPoint).x - Camera.main.WorldToScreenPoint(SelectionBoxCurrentMousePos).x;
     SelectionBoxHeight = Camera.main.WorldToScreenPoint(SelectionBoxMouseDownPoint).y - Camera.main.WorldToScreenPoint(SelectionBoxCurrentMousePos).y;
     SelectionBoxLeft = Input.mousePosition.x;
     SelectionBoxTop = (Screen.height - Input.mousePosition.y) - SelectionBoxHeight;

     //Other Mouse Stuff
     Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);

     if (Input.GetMouseButton(1))
     {
         transform.RotateAround(transform.position, transform.right, -pos.y * MouseRotateSpeed);
         transform.RotateAround(transform.position, Vector3.up, pos.x * MouseRotateSpeed);
         //   transform.RotateAround(transform.position, transform.up, -pos.y * MouseRotateSpeed);
     }
     
     if (Input.GetAxis("Mouse ScrollWheel") > 0)
     {
         GetComponent<Transform>().position = new Vector3(transform.position.x, transform.position.y - .3f, transform.position.z + .2f);
        // transform.Rotate(-2, 0, 0);
     }


     if (Input.GetAxis("Mouse ScrollWheel") < 0)
     {
         GetComponent<Transform>().position = new Vector3(transform.position.x, transform.position.y + .3f, transform.position.z - .2f);
        // transform.Rotate(2, 0, 0);
     }

     if (Input.GetMouseButtonDown(0))
     {
         dragOrigin = Input.mousePosition;
         return;
     }

     if (!Input.GetMouseButton(0)) return;

     Vector3 move = new Vector3(pos.x * dragSpeed, 0, pos.y * dragSpeed);

     transform.Translate(move, Space.World);
 }

 void OnGUI()
 {
     if (Input.GetMouseButton(2))
     {
         GUI.Box(new Rect(SelectionBoxLeft, SelectionBoxTop, SelectionBoxWidth, SelectionBoxHeight), "");
     }
 }

}

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 Puck · Mar 13, 2017 at 10:50 PM

If the second code section is your entire script then I think your problem is probably that you're not using Camera(Pos|Rot)Snap anywhere. You need to assign your variables to cameraObj.transform.location and cameraObj.transform.rotation.

Comment
Add comment · 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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotate object in 90 degrees relative to camera. 1 Answer

3D effect with new UI system 1 Answer

Quick Shortcuts for Rotation 0 Answers

Rotate camera only in 2 directions based on player touch 1 Answer

Turning character with transform.Rotate 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