- Home /
How to rotate an object once?
Hi, I'm new and I still getting used to programming again.
We have a space ship which moves left, right, front, back with the arrows and we want it to move left and right and go 45º rotated when do it and go back to 0º when you stop clicking.
But what we have been able to do is to made it rotate 45º every time we clicked and even if we manage to made it back to 0º, the translation is with space ship axis (rotated 45º) instead the world axis.
I hope I explained myself fine, if not I can provide more info.
Thanks for the help.
=^^= =^^= =^^=
Problem auto solved/shot
Thanks for all the help and sorry for troubles.
The final code is like this
We have a file applied to the player (player_control)
var playerSpeed : float;
//var engineSound : GameObject;
function Start () {
}
function Update () {
transform.Translate(Input.GetAxisRaw("Horizontal") * Time.deltaTime * playerSpeed, 0, Input.GetAxisRaw("Vertical") * Time.deltaTime * playerSpeed);
transform.position.x = Mathf.Clamp(transform.position.x, -45, 45);
transform.position.z = Mathf.Clamp(transform.position.z, -20, 20);
}
And a file aplied to the spaceship (player_angle)
function Start () {
}
function Update () {
if(Input.GetKeyDown(KeyCode.RightArrow)) {
transform.Rotate(Vector3(0.0, 30, 0.0));
}
else if (Input.GetKeyUp(KeyCode.RightArrow)) {
transform.Rotate(Vector3(0.0, -30, 0.0));
}
if(Input.GetKeyDown(KeyCode.LeftArrow)) {
transform.Rotate(Vector3(0.0, -30, 0.0));
}
else if (Input.GetKeyUp(KeyCode.LeftArrow)) {
transform.Rotate(Vector3(0.0, 30, 0.0));
}
}
So now the spaceship rotates, but don't affect to the movement.
Editing your question with your current code would be helpful for anyone answering your question.
This is our code
var playerSpeed : float;
//var engineSound : GameObject;
function Start () {
}
function Update () {
transform.Translate(Input.GetAxisRaw("Horizontal") * Time.deltaTime * playerSpeed, 0, Input.GetAxisRaw("Vertical") * Time.deltaTime * playerSpeed);
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)) {
transform.Rotate(Vector3(0.0, 0.0, -45));
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.RightArrow)) {
transform.Rotate(Vector3(0.0, 0.0, 45));
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)) {
transform.Rotate(Vector3(0.0, 0.0, 45));
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.LeftArrow)) {
transform.Rotate(Vector3(0.0, 0.0, -45));
}
transform.position.x = $$anonymous$$athf.Clamp(transform.position.x, -45, 45);
transform.position.z = $$anonymous$$athf.Clamp(transform.position.z, -20, 20);
}
=^^= =^^= =^^=
Answer by robertbu · Sep 18, 2013 at 06:32 PM
I'm not completely sure of what you are tying to do here. My biggest confusion is that the current movement code moves on the XZ plane, but your rotation code rotates on the 'Z' axis as if the movement should be on the XY plane. Here is a bit of sample code based on my best guess. It moves and rotates (for movement) based on the XY plane. If you want the XZ plane, let me know:
#pragma strict
var playerSpeed : float = 2.0;
function Update () {
var angle = 0.0;
if(Input.GetKey(KeyCode.RightArrow)) {
if (Input.GetKey(KeyCode.LeftArrow))
angle = 0.0;
else
angle = -45.0;
}
else if (Input.GetKey(KeyCode.LeftArrow)) {
angle = 45;
}
transform.eulerAngles = Vector3(0.0, 0.0, angle);
transform.Translate(Input.GetAxis("Vertical") * Time.deltaTime * playerSpeed * Vector3.up);
transform.position.x = Mathf.Clamp(transform.position.x, -45, 45);
transform.position.z = Mathf.Clamp(transform.position.z, -20, 20);
}
Thanks for the help, we are near the answer but still not working. I will try my best explaining myself (I'm not good at it ^^U)
Lets say we have a plane world and a 3D space ship. With the code I put it moves up, down, left and right in the world, but with the rotation, it keeps rotating and ins$$anonymous$$d of moving based on world it moves with the new angle.
With the code you give me it rotates once to left and right but don't move left and right and ins$$anonymous$$d of going up and down in the world it goes up and down based on the spaceship.
first image is my code, second is yours.
=^^= =^^= =^^=
With these kinds of questions, the code the the easy part. Understanding what you want it the hard part. After reading your original question and the above comment, I'm not sure what you want:
What plane does the ship move on...XZ, XY?
Does the ship move with respect to the world or are movements relative to the ship?
If movements are relative to the world, for what purpose is the ship rotating?
How do the movement keys relate to rotation. In particular the arrow keys are also the default keys used for horizontal and vertical movement.
Answer by $$anonymous$$ · Sep 19, 2013 at 01:06 PM
Best thing to do is a trigger on input.GetKeyUp, let's say rotate when key pressed.. When key not pressed rotate back to the original angle.
Thats what I have done. If I clicked it rotates those 45º and when I stop clicking returns to 0º, but ins$$anonymous$$d of moving in the worlds axis, moves in the rotate spaceship axis, so it ends up disappearing under the bg.
I don't know if I'm following the wrong path to made it or I'm just too focused to see the answer u.u
=^^= =^^= =^^=
Reason I'm avoiding getting too much into the script is I only do C# but from my simple understanding of translation. It should look more like this: if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)
not if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)), try it and report back..
Oh and if I'm missing the wind for the tree's post a quick vid.
Your answer
Follow this Question
Related Questions
Wonky Camera Behaviours 0 Answers
How to rotate object from center of the object? 1 Answer
Object is stuttering on rotation 0 Answers
rotate the object based on the two-point 2 Answers