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 SeaPeaMe · Jan 08, 2018 at 08:50 PM · c#rotationquaternioncamera rotate

Camera not rotating

Hey guys! Creeperbot65 here!

I'm having an error with rotating my cut scene camera and it just won't rotate at all no matter what I do! Same thing goes for the door opening animation.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class StartingScene : MonoBehaviour {
 
     public Text DialougeBox;
     private double Timer;
     public AudioClip FootstepSound1;
     public AudioClip FootstepSound2;
     public AudioClip FootstepSound3;
     public AudioClip FootstepSound4;
     public AudioClip DoorOpen;
     public Transform Player;
     private int RandomFootStep;
     public GameObject Darkness;
     public Transform Door;
 
     public bool StartingSceneEnabled;
 
     void Awake () {
         StartingSceneEnabled = true;
         DialougeBox.text = "";
     }
 
     void Update () {
 
         //Increaces the time in seconds for dialouge
         if (Time.time > Timer && Timer < 26) {
             if (Timer != 24) {
                 Timer = Mathf.Round (Time.time + 1);
             }
             if (tag != "Player") {
                 if (Timer < 21) {
                     RandomFootStep = Random.Range (1, 4);
                     if (RandomFootStep == 1) {
                         AudioSource.PlayClipAtPoint (FootstepSound1, new Vector3 (Player.position.x, Player.position.y, Player.position.z));
                     } else if (RandomFootStep == 2) {
                         AudioSource.PlayClipAtPoint (FootstepSound2, new Vector3 (Player.position.x, Player.position.y, Player.position.z));
                     } else if (RandomFootStep == 3) {
                         AudioSource.PlayClipAtPoint (FootstepSound3, new Vector3 (Player.position.x, Player.position.y, Player.position.z));
                     } else if (RandomFootStep == 4) {
                         AudioSource.PlayClipAtPoint (FootstepSound4, new Vector3 (Player.position.x, Player.position.y, Player.position.z));
                     }
                 }
             }
         } else if (Timer > 23) {
             if (tag != "Player") {
                 DialougeBox.text = "";
                 if (Timer == 24) {
                     AudioSource.PlayClipAtPoint (DoorOpen, new Vector3 (Player.position.x, Player.position.y, Player.position.z));
 
                     for (int i = 0; i > 900; i++) {

                                             //Error is here!

                         Door.Rotate (0, 0.10f, 0);
                         Door.position = new Vector3 (Door.position.x + 0.00074444444f, Door.position.y, Door.position.z + 0.00063333333f);
                     }
 
                     Timer = 25;
 
                 }
 
                 if (Timer == 27) {
                     Timer = 28;
                     GameObject MusicBGGameobject = GameObject.Find ("FPSController");
                     BGMusic BGMusicScript = MusicBGGameobject.GetComponent<BGMusic> ();
                     BGMusicScript.MusicSel = 2;
                     BGMusicScript.PlayMusic = true;
                     Instantiate (Darkness, new Vector3 (-0.2f, 1.09f, 8.28f), new Quaternion (0, 0, 0, 0));
                     StartingSceneEnabled = false;
                 }
             }
         }
 
         //The starting texts
         if (tag != "Player") {
             if (Timer == 2) {
                 DialougeBox.text = "Gees, My friends are such a******s.";
             } else if (Timer == 6) {
                 DialougeBox.text = "Forcin' me to go down here or else they'll burn my house down.";
             } else if (Timer == 12) {
                 DialougeBox.text = "";
             } else if (Timer == 15) {
                 DialougeBox.text = "They certainly don't sound like friends now.";
             } else if (Timer == 19) {
                 DialougeBox.text = "";
             } else if (Timer == 23) {

                 //Error is here!

                 float tiltAroundY = Player.rotation.y;
                 float tiltAroundX = 38.29f;
                 float tiltAroundZ = Player.rotation.z;
                 Quaternion target = Quaternion.Euler (tiltAroundX, tiltAroundY, tiltAroundZ);
                 Player.rotation = Quaternion.Slerp (Player.rotation, target, 0.1f);
 
                 DialougeBox.text = "I'm here...";
 
             }
         }
 
         //Scripts for moving player
         if (Timer < 19) {
             Player.transform.position = new Vector3 (Player.transform.position.x, Player.transform.position.y - 0.01f, Player.transform.position.z + 0.01f);
         }
     }
 }
 


Thanks!

-creeperbot65

Comment
Add comment · Show 8
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 TreyH · Jan 08, 2018 at 08:58 PM 0
Share

What is the camera attached to? It looks like the transform named Player is that right?

avatar image TreyH · Jan 08, 2018 at 09:09 PM 0
Share

It's worth mentioning that your lines

 float tiltAroundY = Player.rotation.y;
 float tiltAroundX = 38.29f;
 float tiltAroundZ = Player.rotation.z;

Are storing Quaternion components as Euler components. Was this intentional? In any event, your problem might be:

 Timer = $$anonymous$$athf.Round (Time.time + 1);

Your rotation block is only runs if Timer == 23, but if Timer != 24 then it gets incremented. Quaternion.Slerp() is a smooth linear interpolation that works as you'd expect over a given amount of time, but in your case it's only getting one frame to do the rotation. Have you considered adding the rotation to a coroutine?

avatar image TreyH · Jan 08, 2018 at 09:17 PM 0
Share

Try doing the rotation in a coroutine:


 private IEnumerator LookDownRoutine (float angle = 38.29f, float tolerance = 0.01f)
 {
     // Set up the angle we intend to use and adjust the x component
     Vector3 lookAngle = this.transform.rotation.eulerAngles;
     WaitForEndOfFrame wait = new WaitForEndOfFrame ();
 
     // Check how close we are to the desired rotation
     while ($$anonymous$$athf.Abs (this.transform.rotation.eulerAngles.x - angle) > tolerance) {
     
         // Convert back to a Quaternion and Slerp
         lookAngle.x = angle;
         this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.Euler (lookAngle), 0.1f);
 
         // Then wait a frame
         yield return wait;
     }
 }

Your block would then become:


 } else if (Timer == 23) {
 
         // Use a coroutine here to make sure it works properly over time.
         this.StartCoroutine (this.LookDownRoutine ());
 
         DialougeBox.text = "I'm here...";
     }
 }
avatar image SeaPeaMe TreyH · Jan 11, 2018 at 01:29 AM 0
Share

Could you do it without using this. I'm using the one script for multiple things at once. (I don't know if it would work because I don't understand coroutines)

avatar image TreyH SeaPeaMe · Jan 11, 2018 at 01:34 AM 0
Share

"this" is a C# convention and means you're referring to the class instance attached to a gameobject. It will work on whatever you attach it to.

Show more comments
avatar image SeaPeaMe TreyH · Jan 11, 2018 at 01:45 AM 0
Share

And sadly, The script didn't work :'(

avatar image saschandroid · Jan 11, 2018 at 08:43 AM 0
Share

Typo in line 54:

 for (int i = 0; i > 900; i++)

should be

 for (int i = 0; i < 900; i++)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Milanese · Jan 11, 2018 at 03:29 PM

Hey! I'm not sure if rotting inside a for loop is the best choice. The for loops through before continuing with the next lines of code.

I would put the roation in an update with a counter. So you just have an int i += Time.deltaTime * (your value for speed). then I would put that int as the rotation angle of the axis that you need.

 Void Update()

``{ int i += Time.deltaTime * Speed; Door.rotation = Quaternion.Euler(new Vector3 (0, i, 0)); }

hope this answers your question!

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 SeaPeaMe · Jan 13, 2018 at 03:01 AM 0
Share

I'll have a try, this looks promising, but I think I found my own solution!

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

436 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How to Change rotation while preserving local horizontal rotation 1 Answer

Weird shake when I try to rotate player according to Virtual Cameras rotation. 0 Answers

Flip over an object (smooth transition) 3 Answers

Object Local Y-Up being forced to be World Y-Up 1 Answer

Limit camera RotateAround for an UAV game 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