• 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 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
Add comment · 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 $$anonymous$$ouseLook Script to your Camera and also attach some script to follow your Object. And stop Rotating your transform and ins$$anonymous$$d 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
0

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

@$$anonymous$$ikeNewall - 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

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

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

What's the best alternative to Character Controller? 2 Answers

Random Movement? 1 Answer

Jerky Camera and Varying Translate speeds 1 Answer

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