• 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 TheEmeralDreamer · Aug 29, 2011 at 04:56 PM · movementobject

Objects Randomly Moving

All of the examples I have seen thus far for moving an object relates directly to the user of the application being in control. What If I wanted to move an object controlled by some sort of AI(such as a monster)? How would I go about doing that? I'm having trouble since I can't seem to find an appropriate example of it being done.

Comment
Add comment · Show 3
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 genetixsparkz · Aug 29, 2011 at 05:55 PM 0
Share

what exactly did you have in $$anonymous$$d?

for example if your wanting an A.I. bot to push or throw a large boulder an example would deal with how use the physics engine.

If your wanting to have an A.I. move something that it's holding in it's hand like maybe a cup for example the cup might be easiest by binding it to the hand so it goes where the hand goes.

if your wanting to have an object move through a telekinesis power used by the A.I. you want to change an objects position through modifying its transform. If you notice when you click on an object and see its properties one of them is transform. Transform is an objects position in space as represented by a x,y,z. It's scale as represented as a percentage number of x,y,z and its rotation as represented by degrees for each axis.

A physics example would be how to use the built in physics engine to apply forces to an object. Basically you give an object a rigid body and then use built in functions to mostly just add force, though you can also remove it, normally removal should be done by adding an opposing force ins$$anonymous$$d though, physics engines make movement realistic and in reality releasing the gas doesn't remove force from a car it simply stops adding to it, and braking doesn't remove force it simply increases friction. Increasing friction and then using an equation to simulate friction is much better than lowering it yourself.

A transform example literally moves an objects place on the screen (something that physics engines do to but they use algorithms to do it) it does not however do it in a realistic manner so that's why telekinesis would be a good place to do it, it's ok if an object starts moving and then moves really fast and then comes to a dead stop through a super power.

avatar image genetixsparkz · Aug 29, 2011 at 06:58 PM 0
Share

oh my bad, you were talking about moving a monster, i thought you wanted something to move because of a monster, like A.I. input ins$$anonymous$$d of keyboard/mouse input. my bad

avatar image TheEmeralDreamer · Aug 30, 2011 at 01:21 AM 0
Share

what i mean is this :

function $$anonymous$$ovePlayer() { var controller : CharacterController = GetComponent(CharacterController);

transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);

var forward=transform.TransformDirection(Vector3.forward);

var curSpeed= speed *Input.GetAxis("Vertical");

controller.Simple$$anonymous$$ove(forward * curSpeed);

This is code to move something based on Human input, and that's what confuses me. Ins$$anonymous$$d of accepting input from a controller or keyboard, how would i direct an object to move itself.

transform.Rotate(0, ?????? * rotateSpeed, 0);

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Aug 30, 2011 at 05:27 AM

You can set fixed values instead of using Input.GetAxis - this function return -1 to +1, so any value in this range will do the same as a constant Input.GetAxis. But this has a problem: your character will walk forever, eventually falling from the terrain edge somewhere in time. To avoid this, you must change its direction at intervals. There are several tricks available - doing random rotations at certain intervals is easy to implement. That's a script like yours, but changing to random directions at some fixed interval:

var speed: float = 3; var rotRange: float = 45; // random angle will be between -rotRange and rotRange var timeToChange: float = 5; // change direction each timeToChange seconds

private var timer: float = 0; // start change timer

function Update(){ var controller : CharacterController = GetComponent(CharacterController); timer = timer - Time.deltaTime; // decrements timer counter if (timer speed Time.deltaTime); }

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 TheEmeralDreamer · Aug 30, 2011 at 05:37 AM 0
Share

ah this is a great help. thanks.

avatar image
0

Answer by Meltdown · Aug 29, 2011 at 05:35 PM

There are several ways to implement AI.

One of the simplest way to do so is to create an array of Vector3's that determine the AI's path. So if you wanted a monster to walk around your map, setup the waypoints as Vector3's, and make your monster move towards each one in succession.

For enemy detection, you can make your monster do a Raycast, looking for the player, and change the monsters state to attack or perhaps flee if its health is below a certain level.

I'd suggest first doing the Unity FPS tutorial. There is a section in there on how to implement enemy AI.

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 TheEmeralDreamer · Aug 30, 2011 at 01:18 AM 0
Share

I wasn't really looking for a way to implement AI, what I needed is an example of moving an object that is not controlled by player input.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Animate Environment Objects 1 Answer

How do I fix my player from spinning? 1 Answer

Enemy object spawning before move action is complete :/ 1 Answer

Mobile Application Controls 0 Answers

Making a bubble level (not a game but work tool) 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