Question by
chrisifwax101 · Mar 22, 2017 at 10:56 PM ·
unity 5aihow-tohoverhovercraft
How to make ai move like my character but without controls
I am trying to make a hover craft game and i have a hovercraft that moves perfectly using values from the users controls but i have a problem with getting the AI to move like the main character here is the code i use to control the player
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class CarController : MonoBehaviour {
//Values that control the vehicle
public float acceleration;
public float rotationRate;
//values for faking a nice turn display
public float turnRotationAngle;
public float turnRotationSeekSpeed;
//Reference variables we dont directly use
private float rotationVelocity;
private float groundAngleVElocity;
//Weapons variables
public Transform[] laserSpawnPoints;
public GameObject projectile;
public float ShootForce;
//Initialization code here
void Start () {
}
// Update is called once per frame
void Update () {
Shoot();
}
void FixedUpdate()
{
Move();
}
void Move()
{
//Check if we are touching the ground
if (Physics.Raycast(transform.position, transform.up * -1, 3f))
{
//We are on the ground; enable the accelerator and increase drag
GetComponent<Rigidbody>().drag = 1;
//Calculate forward force
Vector3 forwardForce = transform.forward * acceleration * CrossPlatformInputManager.GetAxis("Vertical");
//Correct force for deltaTime and vehicle mass
forwardForce = forwardForce * Time.deltaTime * GetComponent<Rigidbody>().mass;
GetComponent<Rigidbody>().AddForce(forwardForce);
}
else
{
//We are not on the ground and do not want to just halt in mid-air; reduce drag
GetComponent<Rigidbody>().drag = 0;
}
//You can turn in the air or on the ground
Vector3 turnTorque = Vector3.up * rotationRate * CrossPlatformInputManager.GetAxis("Horizontal");
//Correct force for deltaTime and vehicle mass
turnTorque = turnTorque * Time.deltaTime * GetComponent<Rigidbody>().mass;
GetComponent<Rigidbody>().AddTorque(turnTorque);
//Fake rotate the car when you are turning
Vector3 newRotation = transform.eulerAngles;
newRotation.z = Mathf.SmoothDampAngle(newRotation.z, CrossPlatformInputManager.GetAxis("Horizontal") * -turnRotationAngle, ref rotationVelocity, turnRotationSeekSpeed);
transform.eulerAngles = newRotation;
}
void Shoot()
{
if (Input.GetButtonDown("Fire1"))
{
for (int i = 0; i < laserSpawnPoints.Length; i++)
{
GameObject proj = (GameObject)Instantiate(projectile, laserSpawnPoints[i].position, laserSpawnPoints[i].rotation);
proj.GetComponent<Rigidbody>().AddForce(laserSpawnPoints[i].forward * ShootForce, ForceMode.Impulse);
}
}
}
}
I am new to this please i need help
Comment
Your answer
Follow this Question
Related Questions
Checking when two triggers intersect 1 Answer
How to connect Xcode date And Unity 3d maping? 0 Answers
Endlessly Spinning Drone 1 Answer
Player Collision with the bullet 0 Answers
Unity Animal following pet system 0 Answers