- Home /
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
the rigibody.AddForce(transform.forward, EnemySpeed); is giving me an error under ridgidbody.AddForce, dont exactly know whats wrong
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.
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 ^_^
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.
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)'
Target = GameObject.Find("Player").GetComponent<Transform>();
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