- Home /
How can I make my character move only in one axis or direction at a time?
Hi there, I'm pretty new at this so bear with me please.
I am attempting to write a characterController script that does these two basic functions. 1. To move on the vertical and horizontal axis separately with no diagonal movements. 2. Follow a target such as an immobile training dummy or an AI opponent and always stay squared towards that target whether you move or it moves. Moving in the horizontal axis would form an arc as I want the vertical distance to stay the same until changed. Sp horizontal movement is like circling the opponent.
Here is the code I've been working with from a tutorial, it works for what functions it addresses but they are not the desired results.. There is mouse look code in there that I want to change but I was trying to learn how that worked just because it would be good to know, however it's trickier than just moving around.
using UnityEngine;
using System.Collections;
public class FPS : MonoBehaviour {
public float movementSpeed = 1.75f;
public float strafeSpeed = 1.5f;
public float mouseSensitivity = 5.0f;
CharacterController characterController;
float verticalRotation = 0;
public float upDownRange = 40.0f;
// Use this for initialization
void Start () {
Screen.lockCursor = true;
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
transform.Rotate(0, rotLeftRight, 0);
verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
float forwardsSpeed = Input.GetAxis ("Forwards") * movementSpeed;
float backwardsSpeed = Input.GetAxis ("Backwards") * movementSpeed;
float rightSpeed = Input.GetAxis ("Strafe Right") * strafeSpeed;
float leftSpeed = Input.GetAxis ("Strafe Left") * strafeSpeed;
Vector3 fSpeed = new Vector3(forwardsSpeed, 0, 0);
Vector3 bSpeed = new Vector3(-backwardsSpeed, 0, 0);
Vector3 rSpeed = new Vector3(0, 0, -rightSpeed);
Vector3 lSpeed = new Vector3(0, 0, leftSpeed);
characterController.Move( fSpeed * Time.deltaTime );
characterController.Move( bSpeed * Time.deltaTime );
characterController.Move( rSpeed * Time.deltaTime );
characterController.Move( lSpeed * Time.deltaTime );
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613115523im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving Camera With 2 Players 3 Answers
Everything is choppy... How can I make object movement more fluid? 3 Answers
How to make player move without rigidbody2d 0 Answers
2D movement restriction of x and z 1 Answer
How to make following bubble like facebook messenger on android bubble head chat? C# 0 Answers