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 Nutcrackerz06 · Apr 03, 2012 at 11:12 PM · c#movementaibasic

Basic AI Movement C#

Hello there. I am currently working on a project for my beginning programming class and have a problem. My enemy is supposed to go left and right on the screen and go back and forth while dropping objects toward the player. In my code the enemy goes left and then stops at the edge of the screen but wont go in the other direction. Any help would be appreciated. I am using C#. Thank you for your time.

Code:

using UnityEngine;

using System.Collections;

public class ThiefMove : MonoBehaviour

{

 public int moveSpeed = 140;  //per second 
 Vector3 computerDirection = Vector3.left; 
 Vector3 moveDirection = Vector3.zero; 
 Vector3 newPosition = Vector3.zero; 

 void Start ()  

 { 

  

 } 

 void Update ()  

 {  
     Vector3 newPosition = new Vector3(-1,0,0) * (moveSpeed * Time.deltaTime);  
     newPosition = transform.position + newPosition;  
     newPosition.x = Mathf.Clamp(newPosition.x, -101, 126);  
     transform.position = newPosition; 

     if(newPosition.x > 126)
     {
         newPosition.x = 126;
         computerDirection.x *= -1;
     }

     else if(newPosition.x < -101)
     {
         newPosition.x = -101;
         computerDirection.x *= -1;

     }  
 }  

}

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by raoz · Apr 04, 2012 at 05:01 AM

 Vector3 newPosition = new Vector3(-1,0,0) * (moveSpeed * Time.deltaTime)
 should be
 Vector3 newPosition = computerDirection * (moveSpeed * Time.deltaTime)
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
avatar image
0

Answer by by0log1c · Apr 04, 2012 at 05:20 AM

Like raoz mentions, you'll need to use the computerDirection variables for it to work, but even then, there's a subtle error, I believe.

You clamp the x position between -101 and 126 and right after you are looking for 126. I believe it will never return true, you should use = .

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

Answer by Nutcrackerz06 · Apr 04, 2012 at 10:16 PM

Thank you guys so much for your help. It worked. I appreciate your help. You really helped me out.

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 HolBol · Apr 04, 2012 at 10:16 PM 0
Share

This isn't an answer to the question. Use the Comments section, it's not as if it isn't there.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Adding Wander option to AI 1 Answer

Multiple Cars not working 1 Answer

Simple AI In 2D - C# 1 Answer

Random movement AI 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