Hi there I’d like to add an input.getaxis or input.getaxisraw to the following script I have tried to do it but the player moves way to fast and turn to quick but I’d still like to be able to use this in some way
Heres the script
private MazeCell currentCell;
private MazeDirection currentDirection;
private GameManager GameManager;
public void SetLocation (MazeCell cell) {
if (currentCell != null) {
currentCell.OnPlayerExited();
}
currentCell = cell;
transform.localPosition = cell.transform.localPosition;
currentCell.OnPlayerEntered();
}
private void Move (MazeDirection direction) {
MazeCellEdge edge = currentCell.GetEdge(direction);
if (edge is MazePassage) {
SetLocation(edge.otherCell);
}
}
private void Look (MazeDirection direction) {
transform.localRotation = direction.ToRotation();
currentDirection = direction;
}
public void Start()
{
GameManager GameManager = FindObjectOfType<GameManager>();
}
private void Update () {
GameManager GameManager = FindObjectOfType<GameManager>();
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) {
Move(currentDirection);
}
else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) {
//Move(currentDirection.GetNextClockwise());
Look(currentDirection.GetNextClockwise());
}
else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) {
Move(currentDirection.GetOpposite());
}
else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) {
//Move(currentDirection.GetNextCounterclockwise());
Look(currentDirection.GetNextCounterclockwise());
}
}
adi7b9
December 27, 2019, 1:01pm
2
This behavior is normal in your script. Even with GetAxis you’ll have the same result.
You need to add speed to your player and multiply this speed by DeltaTime.
To move smooth just make a MoveSmoothToPosition(Vector3 destination) method and call it in your move method.
private void Move (MazeDirection direction) {
MazeCellEdge edge = currentCell.GetEdge(direction);
if (edge is MazePassage) {
MoveSmoothToPosition(edge.otherCell.transform.position);
}
}
If you search this forum (or google) you’ll find a MoveSmoothToPosition method. Copy + Paste it and then you can make your own method if you like.
In my opinion is better to use this format
if(...)
{
}
than
if(...){
}
More cleaner i think.
heres an updated script still having issue of it moving to fast with the GetAxisRaw
using UnityEngine;
public class Player : MonoBehaviour {
private MazeCell currentCell;
private MazeDirection currentDirection;
private GameManager GameManager;
public float movementSpeed = 1.0f;
public float rotationSpeed = 200.0f;
public void SetLocation (MazeCell cell) {
if (currentCell != null) {
currentCell.OnPlayerExited();
}
currentCell = cell;
transform.localPosition = cell.transform.localPosition;
currentCell.OnPlayerEntered();
}
private void Move (MazeDirection direction) {
MazeCellEdge edge = currentCell.GetEdge(direction);
if (edge is MazePassage) {
SetLocation(edge.otherCell);
}
}
private void Look (MazeDirection direction) {
transform.localRotation = direction.ToRotation();
currentDirection = direction;
}
public void Start()
{
GameManager GameManager = FindObjectOfType<GameManager>();
}
private void Update () {
GameManager GameManager = FindObjectOfType<GameManager>();
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Joystick1Button3) /*|| Input.GetAxisRaw("Vertical")>0*/){
Move(currentDirection);
}
else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Joystick1Button1)/*||Input.GetAxisRaw("Horizontal")>0*/)
{
//Move(currentDirection.GetNextClockwise());
Look(currentDirection.GetNextClockwise());
}
else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Joystick1Button0)/* || Input.GetAxisRaw("Vertical") < 0*/)
{
Move(currentDirection.GetOpposite());
}
else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Joystick1Button2)/* || Input.GetAxisRaw("Horizontal") < 0*/)
{
//Move(currentDirection.GetNextCounterclockwise());
Look(currentDirection.GetNextCounterclockwise());
}
/*else if (Input.GetKeyDown(KeyCode.Q)) {
Look(currentDirection.GetNextCounterclockwise());
}
else if (Input.GetKeyDown(KeyCode.E)) {
Look(currentDirection.GetNextClockwise());
}
*/
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
GameManager GameManager = FindObjectOfType<GameManager>();
GameManager.EndLevel();
other.gameObject.SetActive(false);
}
if (other.gameObject.CompareTag("Spector"))
{
Debug.Log("Spector got you ");
// other.gameObject.SetActive(false);
Destroy(this.gameObject);
GameManager GameManager = FindObjectOfType<GameManager>();
GameManager.RestartGame();
// End();
}
}
adi7b9
December 30, 2019, 3:20pm
4
This is you’re looking for?
using UnityEngine;
public class Player : MonoBehaviour
{
public float movementSpeed = 1.0f;//1 distance unit per second, should be higher like 20 or even more
private Vector3 nextPosition;
private const float DISTANCE_TO_STOP = 0.001f;
//added later for speed issues (program speed, too much math in Vector3.Distance per update
private bool moveCommand = false;
private void Move (MazeDirection direction)
{
MazeCellEdge edge = currentCell.GetEdge(direction);
if (edge is MazePassage)
{
//MoveTo(edge.otherCell);
if (edge.otherCell != null)
{
nextPosition = edge.otherCell.transform.localPosition;
moveCommand = true;
}
}
}
private void Update () {
// your rest of the code here
MoveSmooth();
}
void MoveSmooth()
{
if (!moveCommand)
{
return;
}
//copy pasted from https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html and modified for your code
transform.position = Vector3.MoveTowards(transform.position, nextPosition, movementSpeed * Time.deltatime);
if (Vector3.Distance(transform.position, nextPosition) < DISTANCE_TO_STOP)
{
nextPosition = transform.position;
moveCommand = true;
}
}
}
Wait!
I think i get it… you need to pause your Move for a sec.
private float waitNextCommandTime = 0.5f;//0.5 seconds
private float nextCommandTimer = 0f;
private void Update () {
GameManager GameManager = FindObjectOfType<GameManager>();
nextCommandTimer += Time.deltatime;
if (nextCommandTimer > waitNextCommandTime)
{
nextCommandTimer = 0f;
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Joystick1Button3) /*|| Input.GetAxisRaw("Vertical")>0*/){
Move(currentDirection);
}
else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Joystick1Button1)/*||Input.GetAxisRaw("Horizontal")>0*/)
{
//Move(currentDirection.GetNextClockwise());
Look(currentDirection.GetNextClockwise());
}
else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Joystick1Button0)/* || Input.GetAxisRaw("Vertical") < 0*/)
{
Move(currentDirection.GetOpposite());
}
else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Joystick1Button2)/* || Input.GetAxisRaw("Horizontal") < 0*/)
{
//Move(currentDirection.GetNextCounterclockwise());
Look(currentDirection.GetNextCounterclockwise());
}
}
/*else if (Input.GetKeyDown(KeyCode.Q)) {
Look(currentDirection.GetNextCounterclockwise());
}
else if (Input.GetKeyDown(KeyCode.E)) {
Look(currentDirection.GetNextClockwise());
}
*/
}
adi7b9:
This is you’re looking for?
using UnityEngine;
public class Player : MonoBehaviour
{
public float movementSpeed = 1.0f;//1 distance unit per second, should be higher like 20 or even more
private Vector3 nextPosition;
private const float DISTANCE_TO_STOP = 0.001f;
//added later for speed issues (program speed, too much math in Vector3.Distance per update
private bool moveCommand = false;
private void Move (MazeDirection direction)
{
MazeCellEdge edge = currentCell.GetEdge(direction);
if (edge is MazePassage)
{
//MoveTo(edge.otherCell);
if (edge.otherCell != null)
{
nextPosition = edge.otherCell.transform.localPosition;
moveCommand = true;
}
}
}
private void Update () {
// your rest of the code here
MoveSmooth();
}
void MoveSmooth()
{
if (!moveCommand)
{
return;
}
//copy pasted from https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html and modified for your code
transform.position = Vector3.MoveTowards(transform.position, nextPosition, movementSpeed * Time.deltatime);
if (Vector3.Distance(transform.position, nextPosition) < DISTANCE_TO_STOP)
{
nextPosition = transform.position;
moveCommand = true;
}
}
}
Wait!
I think i get it… you need to pause your Move for a sec.
private float waitNextCommandTime = 0.5f;//0.5 seconds
private float nextCommandTimer = 0f;
private void Update () {
GameManager GameManager = FindObjectOfType<GameManager>();
nextCommandTimer += Time.deltatime;
if (nextCommandTimer > waitNextCommandTime)
{
nextCommandTimer = 0f;
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Joystick1Button3) /*|| Input.GetAxisRaw("Vertical")>0*/){
Move(currentDirection);
}
else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Joystick1Button1)/*||Input.GetAxisRaw("Horizontal")>0*/)
{
//Move(currentDirection.GetNextClockwise());
Look(currentDirection.GetNextClockwise());
}
else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Joystick1Button0)/* || Input.GetAxisRaw("Vertical") < 0*/)
{
Move(currentDirection.GetOpposite());
}
else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Joystick1Button2)/* || Input.GetAxisRaw("Horizontal") < 0*/)
{
//Move(currentDirection.GetNextCounterclockwise());
Look(currentDirection.GetNextCounterclockwise());
}
}
/*else if (Input.GetKeyDown(KeyCode.Q)) {
Look(currentDirection.GetNextCounterclockwise());
}
else if (Input.GetKeyDown(KeyCode.E)) {
Look(currentDirection.GetNextClockwise());
}
*/
}
thats moves so much better now thank you for the help