• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by verencia_hahaha · Jun 29, 2017 at 05:38 AM · unity 53dshootinginfinite runner

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
         
     }





Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Yanboys · Jul 06, 2017 at 04:56 PM 0
Share

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);
     
 }
avatar image Gruffy Yanboys · Jul 06, 2017 at 11:21 PM 0
Share

I've sorted your original answer here bud, code is now correctly formatted. :)

avatar image
0

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 */
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to to not move when shooting? 0 Answers

Shooting problem Unity3D C# 0 Answers

Glass shader with water illusion 0 Answers

How do I activate an animation when a gameobject enter the Collider of the other animated object 2 Answers

How do i do this type of spline?? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges