Moving gameObject without user input
I am totally new to Unity so please bare with me :) I am trying to make 2D combat scene - Player vs NPC. I have player gameObject which can run both directions jump and has one attack so far. This object has rigidbody2D component, some colliders and it works just fine - does everything what I have made it to do. Then I duplicated this gameObject so that it would be my NPC. It needs only to move left for now therefore I created new controller and new script for NPC. In the script i have this :
var running = false;
var anim: Animator;
var speed: float = 2f;
function Start () {
anim = GetComponent.<Animator>();
}
function Update(){
transform.Translate(Vector2.left * speed * Time.deltaTime);
anim.SetBool("running", true);
}
function OnCollisionEnter2D(coll: Collision2D){
if(coll.gameObject.tag == "player"){
anim.SetTrigger("attack1");
}
}
The problem here is with NPC. First of all it won't move left. It only shows animation because i set "running" to be true. It doesn't show any errors, like the script would be just fine. The other weird thing is that, NPC does not fall down to touch the ground and it seams to be moving up with each animation loop. Ground has edge collider and it works with player gameObject. Bellow section Apply root motion there (which doesn't make any difference if it is checked or not) is a warning - "root position or rotation are controlled by curves". I don't really understand what Unity is trying to tell me, by this.
I also tried this instead of transform.Translate, because I use velocity to move my player character. But it won't work anyway:
var rgb2D: Rigidbody2D = GetComponent.<Rigidbody2D>();
rgb2D.velocity = new Vector2(speed, rgb2D.velocity.y);
I can't seem to figure out what am I doing wrong.
You mentioned you are using rigid bodies. The most common way to make rigid bodies move around is to use the rigidbody AddForce function. (http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html).
Generally, setting the position or velocity yourself, causes problems since the RigidBody component computes it's own motion.
Ok, for now I have commented out script that moves gameObject in order to see if it falls on the ground. It actually does't fall and this might be the reson why running doesn't work by modifying velocity or adding Force.
Gravity on gameObject is set to 1, it is not $$anonymous$$inematic, apply root motion is off for my NPC. If I turn on apply root motion then gameObject moves upwards.
Any ideas what else could cause this?
Answer by Ryzokuken · Sep 13, 2015 at 03:19 PM
You actually need a legit AI script on an NPC to make it do something desired in realtime without Input (the N and P in NPC :P).
Considering your case, the most basic 2D AI would work. I am giving a makeshift AI for you below which you can adapt to your own requirement.
var player : GameObject;
var attackDamage : Integer = 10;
var attackRange : Integer = 15;
var attackSpeed : Integer = 5;
var moveSpeed : Integer = 50;
var animator : Animator;
function Start() {
// To find the player and assign this value to the variable, either using GameObject.Find() or GameObject.FindObjectWithTag()
animator = gameObject.GetComponent(Animator);
}
function Update() {
if(player) {
// If player exists, do all the actions you want your NPC to do. e.g.
var playerDistance : Integer = Vector3.Distance(player.transform.position , transform.position);
if(playerDistance > attackRange) Move();
else Attack();
}
}
function Move() {
// Move towards the player using the moveSpeed and RigidBody2d.velocty
animator.SetBool("isWalking",true);
}
function Attack() {
// Use the attackDamage and attackSpeed to make needed changes
if(animator.GetBool("isWalking")) animator.SetBool("isWalking",false);
animator.SetTrigger("attackTrigger");
}
Thanks Ryzokuken a bunch. I am sure this will help me a lot. However right now it does't solve my problem. The NPC gameObject just won't fall on the ground like player gameObject does, even though it is a copy of the later. So I still can't use velocity or addForce to move my NPC towards player. Player and game object has the same setup except that they uses different controller, script and sprite. I don't think the problem here is related with script.
I$$anonymous$$O, They should fall on the ground all right if:
Both have a collider
Both are rigid bodies 2D
The ground has a collider as well
$$anonymous$$aybe you could give me some background details so that we could solve your problem completely.
I believe conceptually, this would leave you a better scripter.
Answer by Divole · Sep 14, 2015 at 11:39 AM
I found the reason it won't fall on the ground. It showed warning on the NPC gameObject - "root position or rotation are controlled by curves". In the project structure I tried to select each animation so I could see this: Before it also had a button "generate root motion curves" or "remove root motion curves" (something like that). It didn't change anything if I remove or generate root motion curves. However I noticed that the other animations did't have this button. So I deleted that specific animation and recreated it. New animation didn't have this button and the warning on my NPC gameObject disappeared. NPC now falls down on the ground. This somehow probably happened when I duplicated the gameObject.
And then for those who will be looking at this post, I move my NPC using following code:
var rgb2D: Rigidbody2D = GetComponent.<Rigidbody2D>();
rgb2D.AddForce(Vector2.left * speed, ForceMode2D.Force);
Time to make NPC smarter ;)