Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

Moving gameObject without user input

$
0
0
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.(); } 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.(); rgb2D.velocity = new Vector2(speed, rgb2D.velocity.y); I can't seem to figure out what am I doing wrong.

Viewing all articles
Browse latest Browse all 1875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>