- Home /
How : Player move forward automatically (global Z axis) & 'not' the direction it's facing
Hello, I'm a total beginner in using Unity & C#, and I'm just learning by myself. Can anyone help me ?
I would like to make an infinite runner game like Subway Surfers. I want my player to keep on running forward non stop. My player can also rotate and face all directions following mouse, but he then runs forward to the direction he's facing instead. I don't want that.
How can I make my player ignore the directions he's facing, and just continue moving forward ? Is it possible ? By the way I'm using Character Controller and I don't want to use Rigid body.
Answer by verencia_hahaha · Jun 29, 2017 at 05:39 AM
void FixedUpdate() {
transform.position += transform.forward * Time.deltaTime;
Turning ();
}
void Update () {
float horizontalAxis = Input.GetAxis ("Horizontal");
float verticalAxis = Input.GetAxis ("Vertical");
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = new Vector3 (0, 0, verticalAxis);
anim.SetBool ("run", true);
if (Input.GetKeyDown (KeyCode.UpArrow)) {
anim.SetTrigger ("jump");
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move (moveDirection * Time.deltaTime);
Answer by Yanboys · Jul 06, 2017 at 04:54 PM
If you want to make it so that turning around doesn't change the way you're moving, you have to use Rigid body. Then you can toggle the "freeze position" option for x, y, & z (But be sure to only toggle the two you want) and remember to tune the "drag" parameter to suit your needs.
float x;
float y;
private Rigidbody r;
void Start()
{
r = transform.GetComponent<Rigidbody>();
x = transform.eulerAngles.y;
}
void Update()
{
x += Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
//turn around without changing which direction to move
Quaternion rotation = Quaternion.Euler(0, x, 0);
transform.rotation = rotation;
//move forward when you press "w" or "up"
Vector3 forward = new Vector3(0.0f, 0.0f, y);
r.AddForce(forward * 100);
}
Oops. Did that wrong.
float x;
float y;
private Rigidbody r;
void Start()
{
r = transform.GetComponent<Rigidbody>();
x = transform.eulerAngles.y;
}
void Update()
{
x += Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
Quaternion rotation = Quaternion.Euler(0, x, 0);
transform.rotation = rotation;
Vector3 forward = new Vector3(0.0f, 0.0f, y);
r.AddForce(forward * 100);
}
I've sorted your original answer here bud, code is now correctly formatted. :)
Answer by Cornelis-de-Jager · Jul 06, 2017 at 11:27 PM
Instead of using Transform.forward use the Vector3 library:
// Keep running forward no matter rotation
transform.Translate ( Vector3.forward);
You Can use Vector3. Forward, Up and Right. If you want it Back, Down and Left just Multiply by -1:
// Keep running backward no matter rotation
transform.Translate ( Vector3.forward * -1);
Use this as your character controller:
void Update () {
// Will always keep running
transform.Translate (Vector3.forward);
}
//Unity API Collision Function
void OnCollisionEnter (collision other) {
/* TODO: Write Code here to Do When Your Player Hits An Object */
}
Your answer
Follow this Question
Related Questions
Shooting problem Unity3D C# 0 Answers
I Want To Shoot Bullets Correctly. 2 Answers
How to to not move when shooting? 0 Answers
[Unity 3D] How to detect if the game object is left right from the player? 0 Answers
When I look around, my character rotates in a weird circle and it causes it to move around. 0 Answers