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 /
  • Help Room /
avatar image
0
Question by frankmyhre · Sep 26, 2016 at 11:05 AM · unity 5rotationgameobjectreset

Reset rotation of gameobject

Hey,

so I'm trying to reset the rotation of my board-gameobject, with walls as child-objects. There is a ball which the user navigates around a labyrinth and if the ball hits a hole - the ball should have its velocity and position reset and the board should have it rotation reset.

For some reason, I cannot reset the rotation of the board in my Reset-method, which is called when a hole is hit.

this is my code:

 public void Reset(){
     print ("Reset-method called");

     count++;
     setCountText ();

     board.transform.rotation = Quaternion.identity;

     ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
     ball.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;

     ball.transform.position = new Vector3 (startZone.transform.position.x, ballY, startZone.transform.position.z);
 }
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 imran-farooq · Sep 26, 2016 at 12:31 PM

Can't tell exactly what could be an issue but have you tried new vector3(0,0,0)? May be this could work and if it doesn't kindly share more details because your code looks fine.

Comment
Add comment · Show 3 · 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 frankmyhre · Sep 26, 2016 at 01:27 PM 0
Share

Yeah, i tried:

 board.transform.rotation=Quaternion.Euler(Vector3(0,0,0));

And

 board.transform.rotation = Quaternion.Lerp(board.transform.rotation, Quaternion.identity, rotationSpeed*Time.deltaTime)

But the board is resetting its position. I tried to put the code inside the update-method, triggering it when space was hit and the rotation was reset and the $$anonymous$$ute after I released the Space-bar it went back to it's rotated state.

The full code:

 using UnityEngine;

using UnityEngine.UI; using System.Collections; using System;

public class BrainScript : $$anonymous$$onoBehaviour {

 // public variables will be available to the user in the Inspector window.
 public GameObject ball;
 public GameObject board;
 public GameObject startZone;

 public float rotationSpeed;
 public float maxRotation;
 private int count;

 private float ballY;

 private float spinHori = 0f;
 private float spinVert = 0f;

 public Text countText;

 void Start () {
     rotationSpeed = 20.0f;
     maxRotation = 20;

     count = 0;
     setCountText ();
     ballY = ball.transform.position.y;
 }
 
 // Update is called once per frame
 void Update () {

     spinHori -= Input.GetAxis ("Horizontal") * rotationSpeed * Time.deltaTime;
     spinHori = $$anonymous$$athf.Clamp (spinHori, -maxRotation, maxRotation);

     spinVert -= Input.GetAxis ("Vertical") * rotationSpeed * Time.deltaTime;
     spinVert = $$anonymous$$athf.Clamp (spinVert, -maxRotation, maxRotation);

     board.transform.rotation = Quaternion.Euler (spinVert, 0, spinHori);

 }

 public void Reset(){
     print ("Reset-method called");

     count++;
     setCountText ();

     //board.transform.rotation = Quaternion.Lerp(board.transform.rotation, Quaternion.identity, rotationSpeed*Time.deltaTime);

     //board.transform.rotation = Quaternion.identity; 

     board.transform.rotation = Quaternion.Euler (Vector3 (0,0,0));

     ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
     ball.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;

     ball.transform.position = new Vector3 (startZone.transform.position.x, ballY, startZone.transform.position.z);
 }

 void setCountText(){
     countText.text = "Loses: " + count.ToString ();
 }

}

avatar image imran-farooq · Sep 26, 2016 at 01:40 PM 0
Share

Bro you are making a mistake by rotating your board in an update method without applying any check. Actually Update is called continuously even if you call your Reset() method. What you need to do is use a bool for board.transform.rotation = Quaternion.Euler (spinVert, 0, spinHori); Whenever your ball hits the hole this should stop updating too. I hope you got my point, if you still face problem do tell.

avatar image frankmyhre imran-farooq · Sep 27, 2016 at 05:59 PM 0
Share

$$anonymous$$mmhh @imran_farooq ..I think I'm getting the point but I can't seem to implement it.

I created a boolean, and initialized it in the start-method to true.

Then i wrote: if(rotate){ board.transform.rotation = Quaternion.Euler (spinVert, 0, spinHori); }

And the first line in the Reset-method sets the rotate-boolean to false and the last line sets the boolean back to true.

Am I missing something?

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

130 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

Related Questions

Many objects with decimal place in the millionths, can this cause any performance issues? 0 Answers

3D Look at Mouse relative to Camera direction 0 Answers

Destroy(this.gameObject) destroys all the obejcts in the scene, i want only the one that is clicked on 1 Answer

GameObjects 0 Answers

how make object follow mouse cursor in 3d space 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