So you can exert a force of magnitude xx in a particular direction. The same applies to forces, a force has a direction, but also a magnitude (value). So for example, the car can be heading west (direction), with a speed of 20 kilometers per hour (magnitude). The velocity vector give you the direction of the object, while the magnitude, usually referred as speed, will gibe you the value. For example if you think about velocity/speed. I'm happy to help.Ĭlick to expand.The magnitude is the size or length of the vector. If this is still unclear, just give me a shout. If you need to know more about vectors, I have prepared a tutorial here that should get you started fast with these concepts. Which means that the object will translate (or move linearly) forward based on time. Transform.Translate( Vector3.forward * ltaTime) On this page, the example shows the following code In the example provided on the page you mentioned, transform.forward is used to compute the angle between the direction of the object and another object however, it can be used for more basic things such as moving your character forward based on time, using ,for example, anslate (See: ). So, if you were to look at the x,y and z coordinates of this vector, this can be obtained using, , and. Sometimes, it is also useful to obtain the direction of your object (to see where its heading or to perform forward movement) and Transform.forward will provide you with this, as it is a vector that points forward for the object selected.Īs you may already know, each vector has a x,y, and z coordinate in space. This transform will store information on the position, scale, or rotation of your object. If you hover over each of the variables, you should be able to see which variable is equal to zero.Yes, the documentation may be quite confusing at times, especially when you are starting-up with Scripting, so I will try to explain this to the best that I can.įor a given object, you can have a transform. To debug this problem in the future, try inserting a breakpoint at the problematic line and attaching your IDE to Unity. Try changing Update() to FixedUpdate() if you do this you will need to use Time.fixedDeltaTime instead. You could remove ltaTime if #1 doesn't work, but I agree with you that the statement should be in there to keep the game consistent across framerates.Check your project settings/other scripts to make sure Time.timeScale is not equal to zero.Potential Problem 2: ltaTimeĪs said by in the comments, the ltaTime statement might be problematic. (excluding direction) or move the direction assignment statement to your Update() method. That means direction ends up being 0 and is never changed again.Įither change your line to rb2d.velocity = new Vector2(speed * ltaTime, ) You are probably not pressing any keys/moving any joysticks the frame that the scene starts up. You seem to only be setting direction once, in Start(): direction = Input.GetAxisRaw("Horizontal") One of these three variables must be zero to cause your problem. If we look at your code, the problematic statement should be here: rb2d.velocity = new Vector2(speed * direction * ltaTime, ) If someone could help that would be appreciated. Rb2d.velocity = new Vector2(speed * direction * ltaTime, ) rb2d.AddForce(new Vector2(speed * direction * ltaTime, 0)) If ((Input.GetKey(Ke圜ode.LeftShift) || Input.GetKey(Ke圜ode.RightShift)) & ((Input.GetKey("right") || Input.GetKey("d")) || Input.GetKey("left") || Input.GetKey("a")))Įlse if (!(Input.GetKey(Ke圜ode.LeftShift) || Input.GetKey(Ke圜ode.RightShift)) & (Input.GetKey("right") || Input.GetKey("d")))Įlse if (!(Input.GetKey(Ke圜ode.LeftShift) || Input.GetKey(Ke圜ode.RightShift)) & (Input.GetKey("left") || Input.GetKey("a"))) Transform.position = new Vector3(Mathf.Clamp(, -199, 197), Start is called before the first frame updateĭirection = Input.GetAxisRaw("Horizontal") Public class PlayerController : MonoBehaviour Here is the code: using System.Collections I tried using AddForce() instead of changing Rigidbody2D.velocity but that didn't work either, and after 2 or 3 days I still couldn't find a solution. I was making a 2D player controller and it worked until the day after I made a follow camera script, and now my character can't move but the animation still works.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |