• 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 greatUnityGamer · Sep 03, 2013 at 05:17 PM · gamevariablesjava to c#

How do I let my Enemy get the Player's Current Position and other variables?

So usually in games Enemies need to know where the player is.

That would require me to in an Enemy Script to get the player's curernt position constantly.

In java that's easy.

In the Enemies class you make a Player p;

and then in the constructor you set p = the player given in the constructor list

and then in update() theplayerX = p.getPlayerX();


But i know it doesn't work that way in Unity C#. How do i do it?

I'm raedy to start my game and that's one of the basic things i need to do. Get players variables or Get Objects positions

Like other things i would need to know is: =Enemy should know if Player is punching him or if player is running away or if players energy level is low

You know things like that

Comment
Add comment · Show 2
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 Taryndactyl · Sep 03, 2013 at 06:37 PM 0
Share

How many scripts do you have and what are the names for each of them?

We need to know that first because there are different ways of doing it depending upon your setup.

avatar image greatUnityGamer · Sep 03, 2013 at 11:37 PM 0
Share

Taryndactyl well. For my simple AI so far, i would just have 1 for the enemy. One script. In my initial stage of making the AI for the enemy i just want him to be walking around the city and if he sees me he can decide to chase me and attack me or not.

So yeah basically 1 script for now.

For The Enemy it would be EnemyAI
And then for the player I import the FPS script and the mouseLook script so that can look around ike in traditional FPS games.

Then i also want objects like boxes or brick walls or cars to respond to actions from the player or enemies(like for example being pushed or destroyed) so i guess i would make one Script for that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SnowkK · Sep 03, 2013 at 05:35 PM

if what you want is player position on c# you can just use this code

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
     
 public GameObject player;
 public vector3 playerpos; //you need to set the player on the inspector
 
 void update()
 {
 playerpos = player.position;
 
 }
 
 
 
 }
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 greatUnityGamer · Sep 03, 2013 at 05:48 PM 0
Share

Great. so if i set the players position in the inspector, that means the initial position right? cause i mean my player will be moving and so the position will be changing.

And also, that's the position. How about other things like STATES. Like in my Player i will switch to blocking animation if the player blocks. and upon this i will set the state to BLOC$$anonymous$$ING.

so the enemy just needs to get that state variable the same way you told me? So i should make the player's STATE variable public and set it in the inspector?

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

18 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

Related Questions

Error BCE0077 1 Answer

How do I Reset My Variables On GameOver? 1 Answer

getting udp package info inside unity (GlovePIE) 0 Answers

Help with scriptableobjects 1 Answer

join two scripts to use one var in both scripts 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