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);
}
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.
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 ();
}
}
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.
$$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?