hi everyone im trying to make my character jump by himself but i got some error
using UnityEngine; using System.Collections;
public class Enemy : MonoBehaviour {
public float speed = 4f;
public float jetPackSpeed = 0.3f;
public float jumpSpeed = 8f;
public float gravity = 10;
private Transform _Player;
private CharacterController character;
private Transform tr;
private float vSpeed = 0f;
private bool jump = false;
void Start ()
{
_Player = GameObject.FindGameObjectWithTag("Player").transform;
character = GetComponent();
tr = transform;
}
void Update ()
{ // find the vector enemy -> player
Vector3 chaseDir = _Player.position - tr.position;
chaseDir.y = 0; // let only the horizontal direction
float distance = chaseDir.magnitude; // get the distance
if (distance <= 2)
Debug.Log("Attacking Player");
else
{ // find the player direction
Quaternion rot = Quaternion.LookRotation(chaseDir);
// rotate to his direction
tr.rotation = Quaternion.Slerp(tr.rotation, rot, Time.deltaTime * 4);
if (character.isGrounded){ // if is grounded...
vSpeed = 0; // vertical speed is zero
if (jump){ // if should jump...
vSpeed = jumpSpeed; // aplly jump speed
jump = false; // only jump once!
}
}
else // but if lost ground, check if it's an abyss
if (!Physics.Raycast(tr.position, -tr.up, 20f)){ // if no ground below
vSpeed = jetPackSpeed; // use jetpack
}
vSpeed -= gravity * Time.deltaTime; // apply gravity
// calculate horizontal velocity vector
chaseDir = chaseDir.normalized * speed;
chaseDir.y += vSpeed; // include vertical speed
// and move the enemy
character.Move(chaseDir * Time.deltaTime);
}
}
// if collided with some wall or block, jump
void OnControllerColliderHit(ControllerColliderHit hit){
// only check lateral collisions
if (Mathf.Abs(hit.normal.y) < 0.5){
jump = true; // jump if collided laterally
}
}
}
Assets/Robot Artwork/Enemy.cs(19,29): error CS0411: The type arguments for method `UnityEngine.Component.GetComponent()’ cannot be inferred from the usage. Try specifying the type arguments explicitly
To elaborate: The <> are the syntax for defining the specific type to use in generic methods and classes. It also applies for Generic lists, so to declare a list of (Type), you would go List<Type> typeList = new List<Type>(); You can also define your own generic methods! public string ConcatenateNames<T>(T name1, T name2) { return (name1.ToString() + name2.ToString()); }
– syclamoth