![]() If (Vector3.Distance(firingPoint, transform. Start is called before the first frame update This is how I instantiate the bullet: Instantiate(prefab, firingPoint.position, firingPoint.rotation) Īnd this is the bullet class: public class Bullet : MonoBehaviour I will be extremely grateful for every help, this is my first time working with Unity and I'm still trying to learn the ropes.īTW: This is a top down shooter kind of game. I have tried using transform.forward and transform.right but non of them works. I will have a beginner programmer course in the coming future, however this is intended for people who are somewhat familiar with programming and some small familiarity with Unity, but have not made a top down game before.I'm instantiating a bullet out of a gun and trying to move it forward in the direction/rotation is was instantiated as in a 2D space, but for some reason nothing works, the bullet doesn't move or it's moving in the wrong direction. I suggest going through Unity’s official tutorials, starting with the rolling ball tutorial. If you’re not sure what that is, and you are a complete beginner programmer, this might not be the best place for you to start. It’s also important to note all of that code as to go inside the scripts “class”. I did that and got this, which suggests a mistype: Read my code sample again very closely and make sure.Ī useful tip when you get an error is to copy and paste the error message into google – it’s quite common for someone else to have had the error before. It sounds like you have incorrectly declared the ‘Rigidbody2D’ variable, put some code in the wrong place or mistyped something. Vectors are a fundamental mathematical concept which allows you to describe a direction and magnitude. It would be helpful if you could attach that. If you double click the error, it should highlight the section of code. Try adding some debug logging to make sure the movement code isnt being called multiple times if not disable it and make sure no others scripts are moving. ![]() Every Transform component in Unity has local direction vectors defined. So if you plan to make your game work with both keyboards and controllers, check which input is being used and modify your code accordingly – don’t check for diagonal movement with controllers. A Vector2 is typically used to represent a point in 2D space in a Unity game. that will move or rotate in the positive horizontal direction can use Vector.right. For controllers, you don’t need to limit the movement speed because when you press diagonal on a controller it does that automatically. New project settings for both 2D and 3D in Unity support 3D. Then, when we modify our velocity, the new velocity will be 70% slower because we have modified the values of horizontal and vertical to be 70% of their value, so we will be multiplying our runSpeed value by either 0.7 or -0.7, again, setting out speed to 70% of runSpeed. This is why we use a combination of horizontal and moveLimiter. But if horizontal is -1, and we multiply that by 0.7, we’ll end up with -0.7, so we can move at 70% speed to the left. It would always be positive, so you could only ever move right. ![]() Ironically in this case, multiplying horizontal by 0.7 will make horizontal equal to 0.7 or -0.7, but if we just said: horizontal = 0.7f MoveLimiter has the value of 0.7. So when we multiply any number by 0.7, that will give us 70% of that number. ![]() Our if statement checks if you’re moving diagonally – if horizontal and vertical are both not equal to 0, you’re moving diagonally. Next, make a new script and attach it to the character and add the following code inside your class (note there is an improved version at the end of this tutorial so if you’re just copy and pasting, scroll down first – although I strongly suggest you read the article to understand what it does): Rigidbody2D body float horizontal float vertical public float runSpeed = 20.0f void Start () First, attach a Rigidbody2D component to your 2D character, and turn the gravity scale down to 0. If your ship is originally pointing 'up' and rotates due to player input, then you can figure how the direction to move from: Vector2 direction new Vector2 (transform.up.x, transform.up.y) rigidbody2d.velocity direction Time.fixedDeltaTime speed And then for moving laterally, use transform.right.
0 Comments
Leave a Reply. |