Hey, im new to Unity and would like to make my character move. However, i tried some tutorials and nothing worked at all.. i have no clue what im doing wrong since i dont even get errors or warnings. Oh, and i dont want platformer movement, more like from top down. Pretty much like the mobile game "Soul Knight" which im inspired from.
First of all, welcome! Well, could you let us see some of your code? Do you want your character to be controlled with the keyboard? Give us some info!
Well, i got a bit mad because in the vid everything worked just fine and i did exactly the same... but it didnt work at all. So i deleted the project. Yea, i want to control it with the keyboard, W A S D ^^ This is the Tutorial i where i got the code from, i thought this looks okay so i tried www.youtube.com/watch?v=1WG791jyZ6M&index=2&list=WL&t=0s is it maybe because i didnt had a spritesheet? i just set the sprite mode on single
Code (CSharp): if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) // Left movement { [code=CSharp]transform.Translate(Pspeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, 0f); GetComponent<Renderer>().material = lftFace; Pshoot.gunPass.SetActive(false); Pshoot.gun1Pass.SetActive(true); [/code] Here's and example of how I did it. I had to specify which keypress in order to render materials and toggle gun facing side. However, all you need in 2D is Code (CSharp): transform.Translate(Pspeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, 0f); Just be sure to create a float variable Pspeed and start off with about 5f. This is your player speed. If you want to be able to access it inside Unity for quick changes: Code (CSharp): [Header "Player Move attributes"] [SerializeField] private float Pspeed = 5f; //SerializeField makes private variables show up in the inspector The code I provided you with to Input.GetAxis("Horizontal") will make your player controller move Left / Right across the X-Axis with either "A", "Leftarrow", "D", RightArrow. NOTE - You DON'T have to use the GetKey(KeyCode) unless you want certain things to happen upon button press. Just know that GetKeyDown and GetKeyUp both work as well. Depending on your needs. Mine is raycasting so GetKey gives a non stop ray whilst walking. P.S.S. - When you copy/paste code as a beginner programming, take the time to look up the keywords. That will sometimes help you understand what's happening. The rest is just using code over and over and one day you just know the routine. Good luck to ya new programmer!
Thanks for the quick response. However, if i would copy it i would get a bunch of errors whatsoever. For Example, Tupel isnt available in C# 4, use version 7.0 or higher. Does that mean im using a old version of C# ?? How do i update it if so?
What version of Unity are you using? I don't think your C# would need to be updated. If anything, it would be your compiler which might be Monobehavior or Visual Studio. However, I am pretty sure that it will be up-to-date if you have the most recent version. If you are using Unity 4(or anything below 5.3.8) you may need to recode a lot of your scripts because it may be obsolete code in Unity 5+ versions.
I have no clue what was wrong with the other codes that i tried, but this one works just perfect for me. I just hope its a good one which wont cause problems later Code (CSharp): [SerializeField] private float speed; private Rigidbody2D rb; private Vector2 moveVelocity; private void Start() { rb = GetComponent<Rigidbody2D>(); } private void Update() { Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); moveVelocity = moveInput.normalized * speed; } void FixedUpdate() { rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime); }
whoa! im also working on a Soul Knight inspired game, and it would be helpful if u sent me a demo project
@moazzamcxv "...it would be helpful if u sent me a demo project" Code begging in 2 year old thread. There doesn't seem to be anything specific in Soul Knight movement. Any top down movement tutorial will do. So maybe start from https://learn.unity.com/ ? Or YouTube. Or some tutorial you can find with google.