Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges