- Home /
On waypoints if i set the moving speed of player to 20-30 he fall out of terrain why ?
What i mean is that if i set the speed to 10 he will walk between the waypoints without a problem. In this case i clone some cubes and set them as waypoints.
If there is a cube(waypoint) near the terrain edge and the player moving speed is 20 or 30 he will get to the waypoint but then will continue and will fall out of the terrain area instead rotating to the next waypoint. In speed 10 it's working fine.
How can i make that also in high speeds like 20-30 or more he will move fine between the waypoints ?
Another problem is if the cubes small it will walk to next waypoint even if there is a cube in the middle. Like a path find. But if i put huge cubes very big cubes if there is a cube in the middle he will stuck and will not continue to the next waypoint. how can i make when the waypoints objects are big very big the player will turn/rotate left or right and will go around to the next waypoint ?
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class WayPoints : MonoBehaviour {
public GameObject[] waypoints;
public Transform target;
public float moveSpeed = 1f;
public float rotationSpeed = 1f;
private Transform myTransform;
private int targetsIndex = 0;
private Vector3 originalPosition;
public float walkSpeed = 15f;
void Awake()
{
myTransform = transform;
}
// Use this for initialization
void Start()
{
waypoints = GameObject.FindGameObjectsWithTag("ClonedObject");
originalPosition = myTransform.position;
}
// Update is called once per frame
void Update()
{
if (MyCommands.walkbetweenwaypoints == true)
{
WayPointsAI();
}
DrawLinesInScene();
}
private void WayPointsAI()
{
if (targetsIndex == waypoints.Length)
targetsIndex = 0;
target = waypoints[targetsIndex].transform;
float distance = Vector3.Distance(myTransform.position, target.transform.position);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
if (distance < 2f)
targetsIndex++;
}
void DrawLinesInScene()
{
// draw lines between each checkpoint //
for (int i = 0; i < waypoints.Length - 1; i++)
{
Debug.DrawLine(waypoints[i].transform.position, waypoints[i + 1].transform.position, Color.blue);
}
// draw a line between the original transform start position
// and the current transform position //
Debug.DrawLine(originalPosition, transform.position, Color.red);
// draw a line between current transform position and the next waypoint target
// each time reached a waypoint.
if (target != null)
Debug.DrawLine(target.transform.position, myTransform.position, Color.green);
}
}
Your answer
Follow this Question
Related Questions
Why the player is keep moving sometimes out of the grid area ? 0 Answers
How can I slow down to stop(HumanoidIdle) character when getting close to a object using timeline ? 0 Answers
Why the audio sound effects of the main menu are not hearing like in mute ? 0 Answers
Why none of the events OnTriggerEnter or OnCollisionEnter not fire ? 1 Answer