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 Brutalitywarlord · Feb 24, 2015 at 12:16 PM · c#movementobjectenemy

Roll-A-Ball enemyball[Personal Extra] Cant get it to follow the player

Im building onto the roll-a-ball Project,this enemy ball is absolutly extra and isnt part of the project. My idea was to have am Enemy ball that follows you around, i have an idea of what to do when the enemy gets to you, only problem is im having trouble getting to the player, If any of you got any ideas on how to make this code the code i need to make it follow the player then post below :D.

using UnityEngine; using System.Collections;

public class EnemyBehaviour : MonoBehaviour {

 public int EnemySpeed;

 private Transform Target;

 void Start () {
 EnemySpeed = 60;
 Target = GameObject.Find ("Player");
 Target = Target.transform;
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     transform.position=Vector3.MoveTowards(transform.position,Target.position,EnemySpeed);
 }

}

This is the code for the EnemyBall objects script, i am grateful for any help that i get on this, again the objective is to make the enemyball move towards the player, its trying to touch the player so no max distance can be set either

thanks in advance

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 Brutalitywarlord · Feb 25, 2015 at 09:35 PM 0
Share

the rigibody.AddForce(transform.forward, EnemySpeed); is giving me an error under ridgidbody.AddForce, dont exactly know whats wrong

avatar image Brutalitywarlord · Feb 25, 2015 at 10:05 PM 0
Share

nvm you had a spelling error XD

2 Replies

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

Answer by sniper43 · Feb 24, 2015 at 12:28 PM

first thing's first:

 Target = GameObject.Find ("Player");
 Target = Target.transform;


should be

 Target = GameObject.Find ("Player").transform;

for truncation.

Secondly, DO NOT modify positions when using a rigibody unless it ABSOLUTELY what you need. In the case of moving, DON'T use the position.

Use

 transform.LookAt( Target.position );
 rigibody.AddForce(transform.forward, EnemySpeed);


If necessary (it should look nicer) lerp the angle. Here's how it'd look like:

 Quaternion oldRot = transform.rotation;
 transform.LookAt( Target.position );
 transform.rotation = Quaternion.Slerp(oldRot, transform.rotation, 0.5f);
 
 rigidbody.AddForce(transform.forward, EnemySpeed);

Look up how to roll a ball, since that would require using rigidbody.AddTorque and I'm not use what to give as parameters there.

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 Brutalitywarlord · Feb 26, 2015 at 03:05 AM

using UnityEngine; using System.Collections;

public class EnemyBehaviour : MonoBehaviour {

 public int EnemySpeed;
 
 private Transform Target;
 
 void Start () {
     EnemySpeed = 60;
     Target = GameObject.Find("Player").transform;
     
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     rigidbody.AddForce(transform.forward , EnemySpeed);

 }

}

still appears as though there are still errors,,,ill try working them out but extra help is always desired ^_^

Comment
Add comment · Show 9 · 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 meat5000 ♦ · Feb 26, 2015 at 03:08 AM 0
Share

You don't seem to actually use Target for anything. If you are trying to access the transform you should use GetComponent, otherwise you get a snapshot.

AddForce doesn't have an overload that looks like that. It just wants a vector. Change the comma for an asterisk.

avatar image Brutalitywarlord · Feb 26, 2015 at 04:13 AM 0
Share

thanks seems too have solved some errors however now this one pops up Assets/Scripts/EnemyBehaviour.cs(11,37): error CS0120: An object reference is required to access non-static member `UnityEngine.GameObject.GetComponent(System.Type)'

avatar image meat5000 ♦ · Feb 26, 2015 at 04:18 AM 0
Share
 Target = GameObject.Find("Player").GetComponent<Transform>();
avatar image Brutalitywarlord · Feb 26, 2015 at 04:22 AM 0
Share

well that got rid of the errors however the enemy wont follow the player still,,,odd :/ any ideas, its an add-on to Roll-a-ball if that helps,,,objective of my add-on is to make an enemy ball follow you and slow you down on contact

avatar image meat5000 ♦ · Feb 26, 2015 at 04:26 AM 0
Share

That's because you dont use Target for anything.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

How can I make an object in my scene jump continuously? 1 Answer

c# array of game objects movement help 1 Answer

Multiple Cars not working 1 Answer

making an object move to a certain point 3 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