Hi guys, I am trying to make a simple 2D game but have gotten stumped on a what I thought would be a simple movement code.
For the movement code, it has 2 buttons, an up and a down button. The hero is always moving forward, and the buttons change the direction.
Where I am having my trouble is, the down works fine, but the up button instantly raises it to 9.99999 and does nothing else. Is there another way to do this that I am missing?
I am also having trouble with an “if” state for it to only work from 0 to -90 and 0 to 90 degrees each button so it cant move backwards.
I thought this would of worked
if (heroObject.Rigidbody2D.rotation <90)
{
}
but it is saying I’m not referencing to a state of an object (Which I assume is the “Rotation”).
Could I please be pointed in the right direction for this also?
My code is:
var spinSpeed : float = 1; //speed which the object will spin
var forwardVelocityValue : float = 2; //speed the object will move forward
var heroObject : GameObject; //the object this script applies to
function OnGUI ()
{
GUI.Box (Rect (10,10,100,90), "Movement"); //draw a box around the buttons
if (GUI.RepeatButton (Rect (20,40,80,20), "Up")) //spin up button
{
heroObject.transform.eulerAngles.z= spinSpeed; //Object to spin.Startspinning.angle.z = spin speed
}
if (GUI.RepeatButton (Rect (20,70,80,20), "Down"))//spin down button
{
heroObject.transform.eulerAngles.z-= spinSpeed;//Object to spin.Startspinning.angle.z- = spin speed
}
}
function FixedUpdate()
{
rigidbody2D.velocity = transform.right * forwardVelocityValue;;//object to move.velocity = speed (x,y,)
}
I have managed to find a way to get it to work.
If it helps anyone, feel free to use it or what ever.
var spinSpeed : float = 1; //speed which the object will spin
var forwardVelocityValue : float = 2; //speed the object will move forward
var heroObject : GameObject; //the object this script applies to
var maximumUpRotation : float = 80;//The maximum rotation for the Up button to work
var minimumUpRotation : float = 275;//The minimum rotation for the Up button to work
var maximumDownRotation : float = 280;//The maximum rotation for the Down button to work
var minimumDownRotation : float = 85;//The minimum rotation for the Down button to work
function OnGUI ()
{
GUI.Box (Rect (10,10,100,90), "Movement"); //draw a box around the buttons
if (GUI.RepeatButton (Rect (20,40,80,20), "Up")) //spin up button
{
if (transform.localEulerAngles.z < maximumUpRotation) //if rotation is 80 or less then do
{
heroObject.transform.Rotate(0,0,spinSpeed);//Object to spin.Startspinning.angle.z
}
if (transform.localEulerAngles.z > minimumUpRotation) //if rotation is 255 or more then do
{
heroObject.transform.Rotate(0,0,spinSpeed);//Object to spin.Startspinning.angle.z
}
}
if (GUI.RepeatButton (Rect (20,70,80,20), "Down"))//spin down button
{
if (transform.localEulerAngles.z < minimumDownRotation) //if rotation is 85 or less then do
{
heroObject.transform.Rotate(0,0,-spinSpeed);//Object to spin.Startspinning.angle.-z
}
if (transform.localEulerAngles.z > maximumDownRotation) //if rotation is 250 or more then do
{
heroObject.transform.Rotate(0,0,-spinSpeed);//Object to spin.Startspinning.angle.-z
}
}
}
function FixedUpdate()
{
rigidbody2D.velocity = transform.right * forwardVelocityValue;//object to move.velocity = speed (x,y)
}