• 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
Question by Krizarl · Feb 12, 2014 at 11:57 AM · movementtransform

How do I make my Game Object face the direction it's going?

Hi Guys. I'm new to Unity 3D. I've been following the lessons from www.unity3dstudent.com and I've been trying to make my object move using the keyboard. I'm having trouble making it face the direction it's going. Any help and advice would be greatly appreciated. Thanks in advance to anyone who can help me. :)

Here is the script I've been using:

 var speed = 3.0;
 var rotateSpeed = 3.0;
 
 function Update () {
         var horiz : float = Input.GetAxis("Horizontal");
  transform.Translate(Vector3(horiz * rotateSpeed,0,0));
  transform.Rotate(Vector3(0,horiz * rotateSpeed,0));
  {
         var verti : float = Input.GetAxis("Vertical");
  transform.Translate(Vector3(0,0,verti * speed));
  }
  }
Comment

People who like this

0 Show 1
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 sumeetkhobare · Feb 12, 2014 at 04:16 PM 0
Share

This is what I would have done.

You will have to attach a MouseLook Script to your Camera and also attach some script to follow your Object. And stop Rotating your transform and instead get the relative direction in which it has to move compared to your camera.

OR

You have to parent your object with some other object. and move your parent object around, but apply rotation to your child object. For applying rotation, you could look into Lerp() and Slerp() functions.

That's totally what I feel and would have done.

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by robertbu · Feb 12, 2014 at 01:53 PM

Your problem is line 5. You don't want to use the 'Horizontal' axes for both translation and rotation. Line 10 handles the translation.

In addition, this code should scale things by Time.deltaTime so that it runs consistently as the Frames per Second change.

 #pragma strict

 var speed = 3.0;         // Units per second
 var rotateSpeed = 90.0;  // Degrees per second
  
 function Update () {
      var horiz : float = Input.GetAxis("Horizontal");
      transform.Rotate(Vector3(0,horiz * rotateSpeed * Time.deltaTime,0));
 
     var verti : float = Input.GetAxis("Vertical");
      transform.Translate(Vector3(0,0,verti * speed * Time.deltaTime));
  }
Comment

People who like this

0 Show 0 · 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

Answer by VTW · Feb 12, 2014 at 01:56 PM

First, move your var horiz and verti declarations above where your speed and rotateSpeed are. This way you won't keep instantiating a new variable every time your program updates. Instead set horiz within update, or not at all.

You could get rid of horiz and verti and just have "transform.Translate(Vector3(Input.GetAxis("Horizontal") * rotateSpeed....."

As far as rotation goes, what I did (C#) is check if the Input.GetAxis("Horizontal") was > 0. If it was, player was moving right. Else, the player was moving left. You'll want to rotate on the Y-axis via transform.Rotate() within each condition, as that will make your player spin to face another direction.

Comment

People who like this

0 Show 0 · 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

Answer by MikeNewall · Feb 12, 2014 at 01:59 PM

You need to translate in the forward direction of your characters transform.

 transform.Translate(transform.forward * verti * speed);
Comment

People who like this

0 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 robertbu · Feb 12, 2014 at 02:01 PM 0
Share

@MikeNewall - Translate() takes local directions, so passing it a world direction like transform.forward will not work unless you add the Space.World parameter to the Translate().

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

fixed distance for Vector3.moveTowards ? 1 Answer

Jerky Camera and Varying Translate speeds 1 Answer

How to allow movement of game object to certain points only 1 Answer


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