- Home /
making my space ship rotate then move.
so i am trying to make the space ship the player rotate in 90 degree increments then move if it is not already facing in the direction it is told to go.
#pragma strict
var player : GameObject;
var health : int = 100;
var speed : int = 1;
var collided_with : GameObject;
var alignedL : boolean = false;
function Start () {
}
function Update () {
if(Input.GetAxis("Horizontal") >0){
transform.Translate(Vector3(1 * speed * Time.deltaTime,0,0));
}
if(Input.GetAxis("Horizontal") <0){
transform.Translate(Vector3(-1 * speed * Time.deltaTime,0,0));
}
if(Input.GetAxis("Vertical") >0 ){
transform.Translate(Vector3(0,1 * speed * Time.deltaTime,0));
}
if(Input.GetAxis("Vertical") <0 ){
transform.Translate(Vector3(0,-1 * speed * Time.deltaTime,0));
}
}
function OnCollisionEnter(col : Collision) {
collided_with = col.gameObject;
}
i don't just want the answer i would prefer a detailed explenation.
From what I can tell, you never really rotate. So I'm assu$$anonymous$$g you don't know how? Also, you are only translating on the X and Y axis, so this is a 2d game right?
I am a newb but feel like I may be able to help on this one.
When I made a Tetris clone, I encountered a similar situation. I set up a "pivot point" on my blocks and made it the block parent. Whenever I got instructions to rotate the bock from the player, I just rotated the block around the pivot point using transform.RotateAround.
As for a detailed explanation, maybe some clarification would help before I say anything more. Let me know if I am on the right track.
yah that's on the right path i knew transform.rotate i just couldn't figure out how to implement it in a way that worked so it would rotated till it faced the direction it needed to move then begin to move move forward.
Before each "transform.Translate..." add
"transform.localEulerAngles = Vector3(0,0,Z);"
For the Z component of the vector you need 0,90,180 and 270. Just experiment to see which goes where.