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
0
Question by NovaDrox · Dec 18, 2016 at 10:00 PM · movementplayercharactercharactercontroller

How to make my object go in the direction its facing [C#]

I have a basic movement and rotation code, but I want it to go in the direction that the cube is facing. Could someone help me?

using UnityEngine; using System.Collections;

public class Player_Controll : MonoBehaviour { private CharacterController controller;

 public float jumpForce;
 public float moveForce;

 private Vector3 Move_Vector;
 private Vector3 LastMove;
 private float gravity = 14f;
 private float verticalvelocity;

 private Transform CamTrans;

 private void Start()
 {
     controller = GetComponent<CharacterController>();
 }

 private void Update()
 {
     CharacterControll();
 }

 private void CharacterControll()
 {
     if (controller.isGrounded)
     {
         verticalvelocity = -1;

         if (Input.GetKeyDown(KeyCode.Space))
         {
             verticalvelocity = jumpForce;
         }
     }
     else
     {
         verticalvelocity -= gravity * Time.deltaTime;
         
     }

     Vector3 MoveVector = Vector3.zero;
     transform.Rotate(0, Input.GetAxis("Horizontal") * MoveSpeed * 50 * Time.deltaTime, 0);
     MoveVector.y = verticalvelocity;
     MoveVector.z = Input.GetAxis("Vertical") * moveForce;
     controller.Move(MoveVector * Time.deltaTime);
 }

}

Comment
Add comment · Show 4
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 Kossuranta · Dec 19, 2016 at 09:01 AM 0
Share

I haven't really used CharacterControllers, but can't you just rotate the transform?

Look Transform.Rotate from documentation and then just use something like Input.GetAxis("Horizontal") as multiplier with Time.deltaTime and Vector.

avatar image NovaDrox Kossuranta · Dec 19, 2016 at 01:01 PM 0
Share

I have tried using the transform.Rotate, but for some odd reason, its not working.

avatar image GrKl NovaDrox · Dec 19, 2016 at 01:18 PM 0
Share

what have you tried exactly? Show us there is multiple ways of doing this, ans tons on info on the question. If you did not manage to make it work, show us what you did exactly

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Nyhtian · Dec 19, 2016 at 03:59 PM

Doing

     Vector3 MoveVector = Vector3.zero;
     transform.Rotate(0, Input.GetAxis("Horizontal") * MoveSpeed * 50 * Time.deltaTime, 0);
     MoveVector.y = verticalvelocity;
     MoveVector.z = Input.GetAxis("Vertical") * moveForce * transform.forward.z;
     MoveVector.x = Input.GetAxis("Vertical") * moveForce * transform.forward.x;
     controller.Move(MoveVector * Time.deltaTime);
 }``

seems to work.

Comment
Add comment · Show 7 · 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 NovaDrox · Dec 19, 2016 at 08:03 PM 0
Share

When I go to plug that into my code, it doesn't work for some odd reason

avatar image Nyhtian NovaDrox · Dec 20, 2016 at 01:27 AM 0
Share

You have got moveForce at something > 0, right?

avatar image Nyhtian NovaDrox · Dec 20, 2016 at 01:31 AM 0
Share

Oh and just FYI you don't need curly braces on if and else statements if they're only one line.

So you can just do

 if (controller.isGrounded)
      {
          verticalvelocity = -1;
          if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
              verticalvelocity = jumpForce;
      }
 

avatar image NovaDrox Nyhtian · Dec 20, 2016 at 02:06 AM 0
Share

oh, hey, it does work! I don't know why it wasn't before, but now it is. Thank you for the help.

Show more comments
Show more comments
avatar image
0

Answer by Sackstand · Dec 19, 2016 at 02:22 PM

when working with physics (rigedBody) you should use the velocity, MoveTranslation and MoveRotation Method within the FixedUpdate instead of the "normale" transform.

Comment
Add comment · Show 1 · 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 NovaDrox · Dec 19, 2016 at 07:41 PM 0
Share

True, but I am using the character control method.

avatar image
0

Answer by Cambesa · Dec 19, 2016 at 05:35 PM

I'm not sure how the CharacterController works but you could go back to basic and try to move the character with:

 MoveVector.y = verticalvelocity;
 float speed=Input.GetAxis("Vertical") * moveForce;
 float radians=transform.rotation.eulerAngles.y * Mathf.Deg2Rad;
 MoveVector.x = Mathf.Sin(radians) * speed;
 MoveVector.z = Mathf.Cos(radians) * speed;
 controller.Move(MoveVector * Time.deltaTime);

The Cos and Sin might need to get switched if it doesn't work this way.

Kind regards,

Yorick

Comment
Add comment · Show 1 · 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 NovaDrox · Dec 19, 2016 at 08:04 PM 0
Share

I thank you for your help, but it didn't work for some odd reason.

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

93 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

Related Questions

Character Controller problem 0 Answers

Tweaking character movement 1 Answer

How to Logically Match Ground Slope While Using This Code? 1 Answer

Glitchy Movement on Slopes using Character Controller 1 Answer

Moving Platform and CharacterController.Move not working. The player falls 3 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