The question is answered, right answer was accepted
Set new default position each time?
First, I know it's messy code, just bear with me, this is the first project that I was trying to avoid a lot of research so it's a "Try what I know" kinda thing.
Second, So I have this bit of code :
void Update()
{
if (Input.GetKey(KeyCode.W))
{//If "W" Is Pressed, Go Forward .10f
gameObject.transform.position += new Vector3(0, 0, 0.10f);
}
if (Input.GetKey(KeyCode.UpArrow))
{//If "Up Arrow" Is Pressed, Go Forward .10f
gameObject.transform.position += new Vector3(0, 0, 0.10f);
}
if (Input.GetKey(KeyCode.A))
{//If "A" Is Pressed, Go Left .10f
gameObject.transform.position += new Vector3(-0.10f, 0, 0.10f);
}
if (Input.GetKey(KeyCode.LeftArrow))
{//If "Left Arrow" Is Pressed, Go Left .10f
gameObject.transform.position += new Vector3(-0.10f, 0, 0);
}
if (Input.GetKey(KeyCode.S))
{//If "S" Is Pressed, Go Backwards .10f
gameObject.transform.position += new Vector3(0, 0, -0.10f);
}
if (Input.GetKey(KeyCode.DownArrow))
{//If "Down Arrow" Is Pressed, Go Backwards .10f
gameObject.transform.position += new Vector3(0, 0, -0.10f);
}
if (Input.GetKey(KeyCode.D))
{//If "D" Is Pressed, Go Right .10f
gameObject.transform.position += new Vector3(0.10f, 0, 0);
}
if (Input.GetKey(KeyCode.RightArrow))
{//If "Right Arrow" Is Pressed, Go Right .10f
gameObject.transform.position += new Vector3(0.10f, 0, 0);
}
}
Now if you run that, there are no bugs, except if you were to run it, then rotate whatever it's attatched to, to something like 180 degrees, when trying to get forward, it will go backwards. (Because it's in reverse due to 180)
I was just curious if there is anyway to make it so whenever you press the "UpArrow" // "W" it will always go forward.
Thanks in advance, I hope this doesn't go under "Asks for script"; Also have a great day & Merry Christmas :)
Answer by UnityCoach · Dec 25, 2016 at 07:12 PM
You can use transform.Translate () instead of assigning the position property. It has an option for Space, use Space.Self, and it will always move in its own local coordinates.
Merry Christmas!
So would I do gameObject.transform.translate += new Vector3 (0.10f, 0, 0);
etc?
you don't need to specify gameObject, transform is am inherited property of $$anonymous$$onoBehaviour. Translate is a method of the Transform component. You don't assign it a value, you pass it parameters. Something in the like :
transform.Translate(Vector3.up * 0.1f, Space.Self);
Check out the doc here
But what would you do if you don't want it to go up?
I tried : transform.Translate(Vector3.up * 0.1f, 0, Space.Self);
And I was bombarded with errors...
You also want to check out the Input class. When you want to take input, it's much better to use this than hardcoded keys.
Is there any docs that make it able to go slower and to make it so you can only make it go up a certain amount?
$$anonymous$$athf.Clamp allows you to clamp a value between $$anonymous$$ and max values. You can multiply by Time.deltatime to have a framerate independent speed, and multiply the result by a speed multiplier float value that you declare public and that you can adjust in the inspector.
I want it so if you rotate it, it will still go forward on "W" / "Up_Arrow" ? Not a different direction..
No offense, you should take a quick training course on program$$anonymous$$g fundamentals.
This is a one liner :
void Update()
{
transform.Translate (Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0, Space.Self);
}
or if you're in a 3D project.
void Update()
{
transform.Translate (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"), Space.Self);
}
Aye, I'm sorry! This was all on me, When you did "Vector.up" I always kept telling myself "It's always gonna go forward 1, which you added "* 0.1f" which I took it as "It's gonna go forward 1.01" Because I was dumb for a whole day, Lol.
I apologize, when I went to go look it up, it was what I wanted, This was on me, You were right, I was wrong. Sorry for the problem.