Trouble carrying over scripted gravity to NPC.
I'm having trouble getting my NPC to get on the ground. It uses the same asset as the player, the asset doesn't have any inherent components. Here is just enough of my player movement script to give an idea of how I keep it on the ground without a rigidbody.
public class FoxMovement : MonoBehaviour {
public float gravity = 5.0F;
private Vector3 moveDirection = Vector3.zero;
void Start ()
{
}
void Update() {
float movespeed = Input.GetAxis ("Vertical");
CharacterController controller = GetComponent<CharacterController>();
if (Input.GetKey (KeyCode.A))
transform.Rotate (Vector3.up * Time.deltaTime * -rotationSpeed);
if (Input.GetKey (KeyCode.D))
transform.Rotate (Vector3.up * Time.deltaTime * rotationSpeed);
if (controller.isGrounded) {
moveDirection = new Vector3 (0, 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= speed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Here is the current enemy-controlling script.
public class BasicEnemy : MonoBehaviour
{
public Transform target;
public float speed = 3f;
public float attack1Range = 1f;
public int attack1Damage = 1;
public float timeBetweenAttacks;
public Transform myTransform; //current transform data of this enemy
public int rotationSpeed = 3; //speed of turning
// Use this for initialization
void Start ()
{
Rest ();
myTransform = transform;
}
// Update is called once per frame
void Update ()
{
}
}
public void MoveToPlayer ()
{
Debug.Log ("Moving to player.");
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * speed * Time.deltaTime;
}
public void Rest ()
{
}
}
I've tried transferring the gravity references to the way the NPC moves but it's dependent on vector3 and input which the NPC doesn't have. What is the smart thing to do here? Educate me please.
Sticking a rigidbody with gravity on it makes it fall through the world, despite multiple collider configurations.
Ok, to be more concise:
Right now, my NPC flies in a straight line through the air, terrain, and colliders, towards the player's position. I want my NPC to move like my player does, and to keep it on the ground as I do my player.
Any help appreciated.
Follow this Question
Related Questions
Why can my character controlled player can stand on the side of a block? 0 Answers
local Physics 0 Answers
replacement of ControllerCollider 0 Answers
Why does my player weirdly clip into environment when jumping? 0 Answers
I'm making a zero gravity VR game and need help with the movement. Could someone help? 0 Answers