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 /
This question was closed Nov 14, 2016 at 05:14 PM by Thomas-Hawk.
avatar image
0
Question by Thomas-Hawk · Nov 14, 2016 at 03:07 PM · movementcollidercontrollergravityisgrounded

Trouble carrying over scripted gravity to NPC.

I'm having trouble getting my NPC to get on the ground. It uses the same asset as the player, the asset doesn't have any inherent components. Here is just enough of my player movement script to give an idea of how I keep it on the ground without a rigidbody.

 public class FoxMovement : MonoBehaviour {
 
 
     public float gravity = 5.0F;
 
     private Vector3 moveDirection = Vector3.zero;
     void Start () 
     {
     }
 
     void Update() {
         float movespeed = Input.GetAxis ("Vertical");
 
         CharacterController controller = GetComponent<CharacterController>();
 
         if (Input.GetKey (KeyCode.A))
             transform.Rotate (Vector3.up * Time.deltaTime * -rotationSpeed);
         if (Input.GetKey (KeyCode.D))
             transform.Rotate (Vector3.up * Time.deltaTime * rotationSpeed);
         if (controller.isGrounded) {
             moveDirection = new Vector3 (0, 0, Input.GetAxis ("Vertical"));
             moveDirection = transform.TransformDirection (moveDirection);
             moveDirection *= speed;
 
 }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
 
         }
 
 
 }

Here is the current enemy-controlling script.

 public class BasicEnemy : MonoBehaviour
 {
     public Transform target;
     public float speed = 3f;
     public float attack1Range = 1f;
     public int attack1Damage = 1;
     public float timeBetweenAttacks;
     public Transform myTransform; //current transform data of this enemy
     public int rotationSpeed = 3; //speed of turning
 
 
 
     // Use this for initialization
     void Start ()
     {
         Rest ();
         myTransform = transform;
     }
 
     // Update is called once per frame
     void Update ()
     {
     }
     }
 
     public void MoveToPlayer ()
     {
         Debug.Log ("Moving to player.");
         //rotate to look at the player
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
             Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
 
         //move towards the player
         myTransform.position += myTransform.forward * speed * Time.deltaTime;
     }
 
     public void Rest ()
     {
     }
 }

I've tried transferring the gravity references to the way the NPC moves but it's dependent on vector3 and input which the NPC doesn't have. What is the smart thing to do here? Educate me please.

Sticking a rigidbody with gravity on it makes it fall through the world, despite multiple collider configurations.

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 Thomas-Hawk · Nov 14, 2016 at 04:32 PM 0
Share

Ok, to be more concise:

Right now, my NPC flies in a straight line through the air, terrain, and colliders, towards the player's position. I want my NPC to move like my player does, and to keep it on the ground as I do my player.

Any help appreciated.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

100 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Why can my character controlled player can stand on the side of a block? 0 Answers

local Physics 0 Answers

replacement of ControllerCollider 0 Answers

Why does my player weirdly clip into environment when jumping? 0 Answers

I'm making a zero gravity VR game and need help with the movement. Could someone help? 0 Answers


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