I apllied a script to game object to make it as a simple moving enemy and so it works properly but its not touching the ground when moving here is the script
using UnityEngine;
using System.Collections;
public class Enemy ai : MonoBehaviour
{
float distance;
public float lookAtDistance, chaseRange, attackRange, moveSpeed, damping, gravity, attackRepeatTime;
public Transform target;
public CharacterController controller;
private Vector3 moveDirection = Vector3.zero;
private float attackTime;
//TODO: add in a function to find controller and to locate and assign the player as the target
void Start()
{
attackTime = Time.time;
}
void Update()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
LookAt();
}
if(distance > lookAtDistance)
{
GetComponent<Renderer>().material.color = Color.green;
}
if (distance < attackRange)
{
AttackPlayer();
}
else if (distance < chaseRange)
{
ChasePlayer();
}
}
void LookAt()
{
GetComponent<Renderer>().material.color = Color.yellow;
Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
void ChasePlayer()
{
GetComponent<Renderer>().material.color = Color.red;
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime; // doesn't seem to work right :(
controller.Move(moveDirection * Time.deltaTime);
}
void AttackPlayer()
{
//TODO: Need Attack Animations
if (Time.time > attackTime)
{
Debug.Log("Attack");
attackTime = Time.time + attackRepeatTime;
}
}
void ApplyDamage()
{
chaseRange += 30;
moveSpeed += 2;
lookAtDistance += 40;
}
That looks like a problem that judging from the code you should be able to figure out yourself, but you might be looking in the wrong place. Think about this: what exactly do you need to do to make the object touch the ground and what exactly did you DO? Hint: you haven’t even provided the complete code relevant to the problem as far as I can see.
1 Like
Come on, man. This is the same issue you’re working on in the other thread: physics problem with object playing - Getting Started - Unity Discussions
You don’t have to (and shouldn’t) start a new thread just because you fixed one error and another one popped up. In fact, you’re more likely to get help if you update the original thread, since you already have people assisting you.
That said, I agree with @Martin_H . Gotta work through these things yourself a bit. And learn code, man!
2 Likes
You want it just above the ground, you could run into problems if it is at or a little ground level.
Well in the tutorial they uses same code work for them but I also uses same code but doesn’t work . that’s the thing which confuses everyone . I also used same colliders as the tutorial . it is true that I should not always ask someones help but if this kind of known error occurs how I can slove myself
And once again I am confused by your post. What tutorial? That belongs to the information you would need to give in the first post about your problem. @Scheider21 pointed out why you should not have made a new topic for this, it is confusing and counterproductive.
I’m starting to think the reason why I have trouble understanding your posts and you have trouble figuring out your problems is because your thought process is really unstructured.
The official unity tutorials I’ve seen are really good, so if you copied that 100% correctly and it is not doing what it is supposed to do I am thinking that you a) either did in fact NOT copy it 100% correct or b) your usecase is different and you can’t use the code from the tutorial this way or c) the tutorial was for Unity 4.x and Unity 5.x api changes made the code no longer work or something like that.
What “known problem” are you talking about? I don’t see anyone here in this thread saying “yeah sure, that tutorial doesn’t work”.
If you do a tutorial you need to ask yourself “did I understand every single thing in this line of code?” for every line of code you copy. If you answer “yes”, make sure you don’t delude yourself and you actually did understand or it will hurt you in the long run. If the answer is “no” then identify if it is missing knowledge of the API (if so read about that thing in the API documentation) or a C#/general programming problem (google it) or a math problem (google it or read a book on it).
If you do it this way and truly understand the code you are copying from the tutorial you will learn a lot and gain understanding of what to use when and why to use it. And you will also have a better idea of what to do and where to look if the code you write gives unexpected results.
1 Like