Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Divole · Sep 13, 2015 at 01:09 PM · movementgameobjectroot motionwarningcurves

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Sep 13, 2015 at 01:42 PM 0
Share

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.

avatar image Divole · Sep 13, 2015 at 03:02 PM 0
Share

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?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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");
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Divole · Sep 14, 2015 at 09:41 AM 0
Share

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.

avatar image Ryzokuken Divole · Sep 14, 2015 at 11:23 AM 0
Share

I$$anonymous$$O, They should fall on the ground all right if:

  1. Both have a collider

  2. Both are rigid bodies 2D

  3. 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.

avatar image
0

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: alt text 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 ;)


screen-shot-2015-09-14-at-142946.png (16.7 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is root motion reduced once I apply the animation? 0 Answers

How to check if there is a gameobject on a specific gameobject location? 1 Answer

Move object like in scene mode 0 Answers

why OnMouseDown is not working 0 Answers

Making an object follow another object C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges