- Home /
How to make an AI of a car to jump if there is any obstacle
I am doing an game in which the player car can jump using the controls from keyboard. But i want the oppenent car must also jump when there is an obstacle.i have try all the ways like using waypoints .but i did not find the solution how to do it?? please help me..
thanks in advance
Answer by Suyuanhan · Aug 05, 2011 at 06:04 AM
You set a JUMP function ,and set a Collier on the road where you want that car jump,
and you can use OnTriggerEnter() function to execute the JUMP function
this is the script using i did not get the car to jump if any error tell me
using UnityEngine; using System.Collections;
public class AIjump : $$anonymous$$onoBehaviour { public float speed = 6.0F; public float jumpSpeed = 8.0F; public float gravity = 20.0F; private Vector3 moveDirection = Vector3.zero;
// Use this for initialization
void Start ()
{
}
public void OnTriggerEnter(Collider c)
{
print("1");
if(c.gameObject.tag=="enemy")
{
Destroy(c.gameObject);
print("1");
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
moveDirection.y = jumpSpeed;
moveDirection.y -= gravity * Time.deltaTime;
}
}
// Update is called once per frame
void Update ()
{
}
}
Answer by Meltdown · Aug 05, 2011 at 11:02 AM
Suyuanhan's solution could work, although if your level is quite large with lots of obstacles, it is an unpractical solution due to all the colliders and checks you would need to create.
A better solution is to do a Physics.Raycast from the AI car to determine if an obstacle is in the way. (You can do this by tagging all your obstacles and checking if they match that tag). Then you can use the script above for the jump or simply use Rigidbody.AddForce if your AI car has a rigidbody attached.
i have added the raycast and the rigidbody.addforce ,but it is not working here is my code
void Update () { Vector3 fwd = transform.TransformDirection(Vector3.forward); if (Physics.Raycast(transform.position, fwd, 30)) print("There is something in front of the object!"); rigidbody.AddForce(Vector3.up * 100); //rigidbody.AddForce(0,40,0); }
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Smooth turn.... 1 Answer
Unity 5 Standard Asset Car Waypoint AI problem with accuracy 0 Answers
How To Add AI To Car/Racer? 4 Answers
Creating enemy animation in unity 3d 0 Answers