I've been at this for hours and have yet to come up with a solution. Once the player reaches the end goal I would like the player to match the end position and stop moving. ![alt text][1]
I looked into making the player a child of the end goal, but haven't been able to get anything working.
This is my script.
public float speed = 10;
public float turnSpeed = 10;
public float maxVelocityChange = 10;
public float gravity = 10;
public bool isAlive;
public Text Score;
public int CollectibleValue;
//sounds
private AudioSource source;
public AudioClip Spark;
private bool grounded;
private Rigidbody _rigidbody;
private Transform PlayerTransform;
// Use this for initialization
void Start () {
StartLevel ();
CollectibleValue = 0;
}
public void StartLevel(){
PlayerTransform = GetComponent<Transform> ();
_rigidbody = GetComponent<Rigidbody> ();
_rigidbody.useGravity = false;
_rigidbody.freezeRotation = true;
isAlive = true;
source = GetComponent<AudioSource> ();
}
// Update is called once per frame
void FixedUpdate ()
{
if (isAlive) {
PlayerTransform.Rotate (0, (Input.GetAxis ("Horizontal") * turnSpeed * Time.deltaTime), 0);
Vector3 targetVelocity = new Vector3 (0, 0, speed * Time.deltaTime);
targetVelocity = PlayerTransform.TransformDirection (targetVelocity);
targetVelocity = targetVelocity * speed;
Vector3 velocity = _rigidbody.velocity;
Vector3 velocityChange = targetVelocity - velocity;
velocityChange.x = Mathf.Clamp (velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp (velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
_rigidbody.AddForce (velocityChange, ForceMode.VelocityChange);
_rigidbody.AddForce (new Vector3 (0, -gravity * _rigidbody.mass, 0));
}
Score.text = CollectibleValue.ToString ();
}
void OnTriggerEnter(Collider Triggers) {
if (Triggers.tag == "Endgoal") {
isAlive = false;
// Here is where the player needs to stop and match the position of the end goal.
}
After searching, I found what I thought to be the answer, I made a public variable for the targets Transform, and used this line of code.
void OnTriggerEnter(Collider Triggers) {
if (Triggers.tag == "Endgoal") {
isAlive = false;
PlayerTransform.transform.Translate (target.transform.position);
Problem now is, the player get's moved to a position that's not of the target and the player still moves forward. Also if the bool 'isAlive' is false, why is the forward movement still happening?