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 ChoosingOwl · Jun 02, 2014 at 02:11 PM · c#movementenemyfunctioninheritance

Inheritance with enemies

Heyyo guys ! So i've created this code right here, and i wanted to do it so my enemies can inherit the movement and rotation code i've created. But, since im not good with inheritance and so forth it gives me all sorts of errors.

using UnityEngine; using System.Collections;

public class Enemies : MonoBehaviour {

     Transform playerTransform;
     Transform myTransform;
 
 
     protected int Health;
     protected int MoveSpeed;
     protected int Attack;
 
     protected int MovementSpeed;
     protected int RotationSpeed;
     protected float DetectionRange;
     
     void Awake() {
         myTransform = transform;
     }
 
     void Start () {
         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
     }
 
     void Update () {
 
 if (Vector3.Distance(playerTransform.position, myTransform.position) < DetectionRange)
         {
             //Rotate to player
             myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(playerTransform.position - myTransform.position), RotationSpeed * Time.deltaTime);
             
             //Move to player
             myTransform.position += myTransform.forward * MovementSpeed * Time.deltaTime;
             
         }
     }
 
 }

Now these all works well if a put this script onto a gameobject, and give values to movementspeed, detection and rotation. But the moment i do this:

     void Update () {
 
         movement();
     }
     void movement () {
         
         if (Vector3.Distance(playerTransform.position, myTransform.position) < DetectionRange)
         {
             //Rotate to player
             myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(playerTransform.position - myTransform.position), RotationSpeed * Time.deltaTime);
             
             //Move to player
             myTransform.position += myTransform.forward * MovementSpeed * Time.deltaTime;
             
         }
     }

(I moved my movement and rotation code in a separate function). The reason for why im doing this is, that enemies should inherit these traits, and therefore making it easier for me to create enemies my adjusting the variables RotationSpeed, MovementSpeed and DetectionRange for each enemy that inherits this. In general i want this function problem solved, so i can begin to create script that inherit these traits properly, but i'm having a hard time figuring it out. Could you help me ?

Kind Regards, Lars.

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

1 Reply

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

Answer by PouletFrit · Jun 02, 2014 at 03:20 PM

If you want that each enemy have its own movement function you just have to declare a virtual method on the parent class.

 protected virtual void movement() {
     // Your basic rotation and movement code here
     // This function will be used if you't dont override it in child classes
 }

And then in your child class, just override this function

 public class Slime : Enemies {
     protected override void movement() {
         // Movement code that is specific to this enemy type
     }
 }

Also, if you wish to call the function of the parent inside the child function just use:

 base.movement();

Also, I have noticed that you didnt specify the access modifier (public, private, protected, etc..) for the variables playerTransform and myTransform, which mean that by default in c# they will be private and thus not accessible in your child classes. If you wish to use them in your child classes, switch them to protected.

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 ChoosingOwl · Jun 02, 2014 at 03:57 PM 0
Share

Hey ! Thanks that worked a bunch ! I missed the whole private/public on my transform variables when trying to do it.

$$anonymous$$ind Regards, Lars.

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

22 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

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

SetDestination function error 0 Answers

Multiple enemy prefabs moving towards a single goal. Need help! 1 Answer

Enemy moves randomly within a area. 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