• 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
Question by Vice_Versa · Dec 08, 2015 at 05:02 PM · rotationmovementpositionaitransform.translate

how to move an object, but make it stay on the ground

im working on an enemy in my game that will shoot the player from a vertain distance, but move towards him and use melee attacks if it gets closer. everything for it is working so far except when my player jumps, the enemy follows it in the air. The line im using to make it follow the player is transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

the problem is that im also making him rotate towards the players location so it can shoot from a distance

Quaternion rotation = Quaternion.LookRotation (target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * speed);

so how can i make him move toward the players location, but not be floating in the air to follow him when he jumps?

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by wibble82 · Dec 08, 2015 at 05:12 PM

Hi

By default transform.Translate operates in local space, hence you being able to use 'vector3.forward' - i.e. move along your own forward (z) axis. That works fine until, as you say, your player jumps!

As you want to do something based on world space now ('up' from the point of view of the world) you'll need to start moving based on world space values. The simplest way I can see of writing this code in your game might be:

 //get our forward in world space
 Vector3 my_forward = transform.forward;
 
 //clear the y so we dont move up or down
 my_forward.y = 0;
 
 //normalize it, so we have a unit vector even after chopping out the y bit
 my_forward = my_forward.normalized;
 
 //scale it by the amount we want to move
 my_forward *= moveSpeed * Time.deltaTime;
 
 //and do our translation in world space 
 //(could be written transform.position += my_forward)
 transform.Translate(my_forward, Space.World);
 
 //or on much fewer lines!
 //Vector3 my_forward = transform.forward;
 //my_forward.y = 0;
 //transform.position += my_forward.normalized * moveSpeed * Time.deltaTime;
 

Lots of ways to write it that may vary depending on your situation, but that's the basic concept - get the direction you want to move, chop out the bits you don't want, renormalize it and then move by however much you want.

-Chris

Comment
Bonfire-Boy

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

42 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

Related Questions

Setting z value of trans.pos relative to rotation 1 Answer

How to predict position of object before transform.translate will apply? 1 Answer

How to stop a unit from rotation once it gets to designated position? 2 Answers

Interpolating the motion of a camera moving from one node to another 1 Answer

How to make camera move backwards in relation to player (so behind player), regardless of y rotation of player and camera 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