Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by JHM007 · Sep 29, 2014 at 09:33 AM · c#errortransform.position

An NPC "moonwalks"

As part of my newly started education in gamedevelopment I encoutered a peculiar issue: we have each progammed an NPC so when the player came within a certain distance of the NPC, the NPC would react by moving towards the player. However, in my project, the NPC "moonwalks" away from the player, a development which have left my teacher stumped.

Here is the code in question, the programming language is C#:

 using UnityEngine;
 using System.Collections;
 
 public class Troll : MonoBehaviour {
 
     Animation anim;
     GameObject player;
 
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animation> ();
         anim.Play ("Idle_02");
         player = GameObject.FindGameObjectWithTag ("Player");
     }
     
     // Update is called once per frame
     void Update (){
         
         if (Vector3.Distance (transform.position, player.transform.position) < 3) {
                         Vector3 dir = new Vector3 (player.transform.position.x, transform.position.y, player.transform.position.z);
                         transform.LookAt (dir);
                         anim.Play ("Attack_01");
                         rigidbody.velocity = new Vector3 (0, rigidbody.velocity.y, 0);
                 } else if (Vector3.Distance (transform.position, player.transform.position) < 20) {
                         Vector3 dir = new Vector3 (player.transform.position.x, transform.position.y, player.transform.position.z);
                         transform.LookAt (dir);
                         anim.Play ("Run");
                         rigidbody.AddForce (transform.forward * 100);
                 } else {
                         anim.Play ("Idle_01");
                 }
         }
     }
 

An odd detail is that if I change "rigidbody.AddForce (transform.forward * 100);" to a negative number, the NPC will move forward. The NPC is the "Earthborn Troll" from the asset store, in case that is an important detail.

I would greatly appreciate any help.

Comment
Add comment · Show 1
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 dmg0600 · Sep 29, 2014 at 09:44 AM 0
Share

It works in a newly created project with just that script, a cube to make the floor, a cube with this script and a FirstPersonController with the tag Player.

Check your project for what it might be changing the behaviour. If at last you don't find what it is and you just have this in your proejct, start again with a clean proejct.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by bubzy · Sep 29, 2014 at 09:41 AM

you may want to try this

http://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

if you add a force to the rigidbody, you are going to have to slow it down too, while this may work well for solid objects, an NPC may suffer from it, unless of course you want them to have some sort of inertia. as for why your code doesn't work, it may be worth checking out the actual direction the model is looking in (not what it looks like hes looking at).

hope this helps a little bit.

Comment
Add comment · Show 1 · 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 bubzy · Sep 29, 2014 at 09:44 AM 0
Share

additionally, as an aside, i (personally) would use variables for the attack distance like

float attackDistance = 3f;

float awareDistance = 20f;

it may also be worthwhile looking at a state$$anonymous$$achine for the ai as it is possible that both of your conditions may be met.

avatar image
0

Answer by JHM007 · Sep 30, 2014 at 11:26 AM

Thank you all for the input, but as it turns out, the fault was my own and rather... stupid. I had mistaken "Freeze Position" with "Freeze Rotation" under "Rigid Body".

So thanks again for the help.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable 3 Answers

I can do this in JS or not ??? 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Adding value to GameObject[] 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