- Home /
how to make a rotating object move in a specific direction on press of a button?
i want to make a game object (a cube) move in the direction of its head(any corner) when a button is pressed very much like a kite.i am new to controller scripting(i have the basic java knowledge) so ill be glad of any help .while to object is rotating in a circle ,the object must move in the direction of the head on the pres of a button.thanks in advance.
iam using this script
static var speed : int =5 ;
function Update () {
if (Input.GetKey (KeyCode.UpArrow)) transform.Translate (Vector3(0,1,0) * Time.deltaTime*speed);
if (Input.GetKey (KeyCode.DownArrow)) transform.Translate (Vector3(0,-1,0) * Time.deltaTime*speed);
if (Input.GetKey (KeyCode.LeftArrow)) transform.Translate (Vector3(-1,0,0) * Time.deltaTime*speed);
if (Input.GetKey (KeyCode.RightArrow)) transform.Translate (Vector3(1,0,0) * Time.deltaTime*speed);
}
Answer by malekbakeer · Aug 17, 2014 at 11:56 AM
static var speed : int = 5;
function Update ()
{
if (Input.GetKey (KeyCode.UpArrow))
transform.position.y = Mathf.Lerp (transform.position.y, transform.position.y + 1, Time.deltaTime * speed);
if (Input.GetKey (KeyCode.DownArrow))
transform.position.y = Mathf.Lerp (transform.position.y, transform.position.y - 1, Time.deltaTime * speed);
if (Input.GetKey (KeyCode.LeftArrow))
transform.position.x = Mathf.Lerp (transform.position.x, transform.position.x + 1, Time.deltaTime * speed);
if (Input.GetKey (KeyCode.RightArrow))
transform.position.x = Mathf.Lerp (transform.position.x, transform.position.x - 1, Time.deltaTime * speed);
}
if you are pressing a button and it does the reverse... try reversing the "-" and "+" marks one at a time
no i just want the object to move in one dirction and not like a third person controller . it is actually rotating in a circle and i want to make it move tangentially when when you press the botton.(imagine flying a kite)
Answer by itsharshdeep · Aug 17, 2014 at 03:09 PM
`Use inbuilt "Horizontal" & " vertical" axis
With
Float inputX= Input. GetAxis ("Horizontal");
Float inputZ = input.GetAxis (" Vertical");
Vector 3 direction = new Vector 3 ( inputX*speed* time.deltaTime, 0, inputZ*speed*Time. deltaatime)
Transfotm translate(direction);
There might be some errors or mistakes in this . So must google it once
no i just want the object to move in one direction and not like a third person controller . it is actually rotating in a circle and i want to make it move tangentially when you press the button.(imagine flying a kite). but ill try it.
can u provide any screen shot of ur game/ or scene view
i think the method i told u, will work ..pls provide some more data
ok so i basically want it to move it only the direction it is pointing,i.e,making the pointing object move in the pointed direction(up,down,right or left).like in the screenshotsalt text
Your answer
Follow this Question
Related Questions
How to change the angle my character falls at 1 Answer
Rotating Player 0 Answers
2d Movement Messes up when cube is rotatated 0 Answers
How to make an object go the direction it is facing? 6 Answers