![]() While earlier, we used to update any component's property using the Inspector view. Now we know that we can access a gameObject in the script attached to it by using the reference gameObject.Īlso, we can access and Component(like RigidBody, Collider etc) attached to the gameObject using the GetComponent function. Since pretty much everything we've written in this script deals with movement by position change, let's clear everything up so we have a clean Start() and Update() methods again. adding speed value to the X axis position getting the initial position where prefab is createdįireballXValue = ![]() Public class FireballBehaviour : MonoBehaviour Open up the script that defines our bullet/fireball's behaviour: Let's explore this option, and, we'll understand the AddForce() method provided by the class Rigidbody. Instead of doing that, what if we applied a very strong impulsive force to the bullet, much like the one in real life? Doing so will make the bullet move because of its momentum, and not because of a programmed position update. It simply didn't know that it was supposed to stop, after hitting the target, since we programmed it to keep going to the right. Which also leads to issues like the one in which our bullet kept going even after it hit the target(Checkout our previous tutorials for the example). We've worked with shooting bullets so far, right? But our bullets have only travelled using the position change regardless of what they are and how they move in real world. In this tutorial we will cover an interesting example of using Rigidbody Physics for movement instead of Position change trick. ![]() Rigidbody Physics: When dealing with objects that follows physics rules, it makes more sense to apply forces on objects or change their velocity instead of a direct position update.This is what we've been doing so far, by simply adding a value to the object's X position every frame. Changing Position Coordintes: By directly changing the position of a gameObject without much consideration to its physics or other such components.There are mainly two ways of moving a gameObject in Unity: RigidBody Movement: velocity and AddForce(). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |