- Home /
I need help with rotation
Hey, I am making a game with a cannon that you can rotate to then shoot at cubes, Although when I change the rotation of the cannon, it just faces down and won't even rotate when I press the buttons I created. Any help would be good, here is my code :
//var firePower : float;
var ball : Transform;
var ballPos : Transform;
var timer : float;
var addTime : boolean;
var theBall;
var move : boolean;
var rotationX : int = 270;
var barrel : Transform;
function Update(){
barrel.rotation.x = rotationX;
if(!addTime){
if(Input.GetKeyUp(KeyCode.Space)){
fire();
addTime = true;
}
}
if(move){
theBall.rigidbody.AddForce(-Vector3.forward * 50);
}
if(addTime){
timer += Time.deltaTime;
}
if(timer > 1){
addTime = false;
timer = 0;
theBall = GameObject.Find("Sphere(Clone)");
Destroy(theBall.gameObject);
}
}
function fire(){
Instantiate(ball,ballPos.position,ballPos.rotation);
move = true;
theBall = GameObject.Find("Sphere(Clone)");
}
function OnGUI(){
if(GUI.Button(Rect(10,10,100,30),"+")){
rotationX += 3;
}
if(GUI.Button(Rect(10,40,100,30),"-")){
rotationX -= 3;
}
}
Answer by robertbu · Oct 26, 2013 at 09:02 PM
Your problem is you are directly addressing 'rotation'. See my answer at the link below:
http://answers.unity3d.com/questions/563288/object-rotation-not-clamped-properly.html
For your specific use here, I suggest you use a relative rotation rather than trying to deal with euler angles. Something like:
transform.Rotate(3,0,0);
http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html
'X' rotation in particular have the issue mention in my first link where Unity will change representation. As long as you don't read Transform.eulerAngles, you should be okay, but you need to set all three axes at the same time. You should not set just a single axis. Transform.Rotate() will work.
Rotate() works fine for the ball spawn point, but when I fire the balls they still go straight no matter what rotation the spawn point is on, Here is an example.
Answer by Bincredible · Oct 27, 2013 at 11:13 AM
I fixed it! I had to change the AddForce to the spawn point's forward not Vector3.forward.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Instantiating objects at position 1 Answer
Movement Relative to both Camera and XZ Plane 3 Answers
Objects Instantiating at wrong position 3 Answers
Rotation problem with spawning bullet 2 Answers