- Home /
What the variable motorSetter and motor type should be ?
This is how my WayPoints script was in the original
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");
foreach(GameObject go in waypoints)
{
SphereCollider sc = go.AddComponent<SphereCollider>() as SphereCollider;
sc.isTrigger = true;
}
foreach (GameObject go in waypoints)
{
go.AddComponent<SlowDown>();
}
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);
}
}
Then i created more one script
using UnityEngine;
class SlowDown : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
Debug.Log("Object " + other.name + " entered " + name);
WayPoints.SetSpeed(WayPoints.moveSpeedSlowed);
}
private void OnTriggerExit(Collider other)
{
Debug.Log("Object " + other.name + " exited " + name);
WayPoints.SetSpeed(WayPoints.moveSpeed);
}
}
Now i need to use the SlowDown script with my WayPoints script so i added to the WayPoints script:
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;
//setters for setting everything in Unity editor
public float moveSpeedSetter = 1f;
public float moveSpeedSlowedSetter = 0.5f;
public GameObject motorSetter;
//static objects changeable via any script
public static float moveSpeed;
public static float moveSpeedSlowed;
public static motor;
public static void SetSpeed(float speed)
{
motor.movement.maxForwardSpeed = speed;
}
public float walkSpeed = 15f;
void Awake()
{
myTransform = transform;
}
// Use this for initialization
void Start()
{
waypoints = GameObject.FindGameObjectsWithTag("ClonedObject");
foreach(GameObject go in waypoints)
{
SphereCollider sc = go.AddComponent<SphereCollider>() as SphereCollider;
sc.isTrigger = true;
}
foreach (GameObject go in waypoints)
{
go.AddComponent<SlowDown>();
}
moveSpeed = moveSpeedSetter;
moveSpeedSlowed = moveSpeedSlowedSetter;
motor = motorSetter;
motor.movement.maxForwardSpeed = moveSpeed;
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);
}
}
But first i'm getting errors on the motor and motorSetter variables. They should not be GameObject type and not ThirdPersonCharacter and also not sure about giving it the name motor and motorSetter.
But how do i fix this errors and how do i combine the SlowDown with the function WayPointsAI ?
What i want to do is that when the character(ThirdPersonController) is moving between the waypoints when getting close to a waypoint to slow down and then after making the turn/rotate to the next waypoint speed up back to the moving speed he was. Even if the user is changing the speed between waypoints always after the next waypoint slow down then speed up to the last speed he was moving.
But i messed it all up with the SlowDown script and the new variables in the WayPoints script.
Answer by metalted · Feb 27, 2017 at 10:11 PM
public static void SetSpeed(float speed)
{
motor.movement.maxForwardSpeed = speed;
30. }
You are calling movement.maxForwardSpeed on something you don't know the type of... How did you come to this? There has to be a reason to place movement.maxForwardSpeed after a variable, otherwise it is as logical as saying
transform.makeMe.aSandwich();
Anyway, when you are coding, try not to blindly copy code (not saying you did) from a website or someone else but really try to understand what the code does. This makes it easier to handle errors when testing.
public static void SetSpeed(float speed)
{
motor.movement.maxForwardSpeed = speed;
30. }
You are calling movement.maxForwardSpeed on something you don't know the type of... How did you come to this? There has to be a reason to place movement.maxForwardSpeed after a variable, otherwise it is as logical as saying
transform.make$$anonymous$$e.aSandwich();
Anyway, when you are coding, try not to blindly copy code (not saying you did) from a website or someone else but really try to understand what the code does. This makes it easier to handle errors when testing.
After some googling, I found that "movement.maxForwardSpeed" comes from the Character$$anonymous$$otor.js script in the Unity Standard Assets. When we look into the script, we can see that the class name containing these variables is "class Character$$anonymous$$otor$$anonymous$$ovement". (at line 22).
This means that when you are calling something on "motor", you are calling on the Character$$anonymous$$otor$$anonymous$$ovement class. The type of motor is therefore: Character$$anonymous$$otor$$anonymous$$ovement:
public Character$$anonymous$$otor$$anonymous$$ovement motor;
sources: http://answers.unity3d.com/questions/893452/where-is-the-function-charactermotormovement-creat.html
http://answers.unity3d.com/questions/147622/need-script-i-overwritten-my-character-controller.html
I hope this solves your problem, cause I have never used Character$$anonymous$$otor$$anonymous$$ovement before, but this is my best guess. Good luck.
Your answer
![](https://koobas.hobune.stream/wayback/20220612112105im_/https://answers.unity.com/themes/thub/images/avi.jpg)