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 SuperBarrio · Jul 01, 2011 at 03:47 PM · axislookatforwardface

Face the direction of movement.

While I know almost nothing about JavaScript, I have managed to cobble together a code that will move a game object (The player. I use a rigidbody) when a GUI button is pressed.

The purpose of this is to allow me to try and make a simple game that could theoretically be used on a touch screen device by way of a “Virtual D-pad”

While it works thus far – I am unable to get the object to rotate and face the direction that it is moving.

If it is of any benefit – The character moves in the X and Z axis along the ground.

This is code used:

 var customButton : GUIStyle;
 var person : GameObject;
 
 function OnGUI () {
 
     // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
     if (GUI.RepeatButton(Rect (0,180,Screen.width / 2,60), "", customButton)) 
 {
         person.transform.Translate(-0.05,0,0);
         
 }
 }

As it stands, I use 4 variants of this code as separate JavaScript objects to control the player (Condensing them into one will be the next stage, but if you think it would be simpler to do now, please do not hesitate to say!)

Any further details and I will happily try to give them.

Thanks

I also apologise that this is similar to many other questions, but I tried to implement the various solutions to no effect.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by flaviusxvii · Jul 01, 2011 at 03:51 PM

person.transform.forward is the direction the person is facing. use person.transform.LookAt(some position in the world) to get it to face that way. Do something like person.transform.position += person.transform.forward Time.deltaTime speed to move the person in the direction he is facing.

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 SuperBarrio · Jul 02, 2011 at 04:48 PM 0
Share

LookAt is a pretty solid soloution – However I now have one smaller issue.

When looking at an object, when the player character moves out of the central X and Z axis of the level (assu$$anonymous$$g the look at objects are in the middle) the character will veer directly towards that point when using

“transform.position += person.transform.forward Time.deltaTime speed;”

as opposed to what I have above, it also happens if I do not change it.

Simply adding..

var point: Vector3 = targetObj.position;

and

person.transform.LookAt (point);

..to my original script seemed to do the trick, but it still veers very slightly towards the target,and while not such a problem as to ruin the game play, is not ideal.

To summarise:

The character looks at the target but now moves in the direction desired, as well as drifting ever so slightly off course.

Hope this is clear!

avatar image
0

Answer by aldonaletto · Jul 01, 2011 at 04:09 PM

Instead of moving the character in each direction separately, you can turn the character in the direction you want, then move it always forward:

 var speed:float = 4;
 
 if (GUI.RepeatButton(Rect (0,180,Screen.width / 2,60), "", customButton)) 
 {
         person.transform.forward = -Vector3.right;
         person.transform.Translate(0,0,speed*Time.deltaTime);
 }
 if (GUI.RepeatButton(Rect (...), "", customButton)) // right
 {
         person.transform.forward = Vector3.right;
         person.transform.Translate(0,0,speed*Time.deltaTime);
 }
 if (GUI.RepeatButton(Rect (...), "", customButton)) // forward
 {
         person.transform.forward = Vector3.forward;
         person.transform.Translate(0,0,speed*Time.deltaTime);
 }
 if (GUI.RepeatButton(Rect (...), "", customButton)) // back
 {
         person.transform.forward = -Vector3.forward;
         person.transform.Translate(0,0,speed*Time.deltaTime);
 }

Using Time.deltaTime you can have a constant velocity, independent of frame rate. You can test other values for speed in the Inspector.
This is not the most efficient code in the world, but match what you already had.

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 SuperBarrio · Jul 02, 2011 at 06:01 PM 0
Share

Thanks, Time.deltaTime seems like a favourable option - and I hope to re-impliment it (I'd have used this version of the code but it gave me a lot of simple erros I lack the skill to unravel.)

As it stands, moving forward at the moment seems to be causing me to veer slightly to one side when the character rotates, but I will continue to tinker - Thankyou

(Sidenote: While I know very little, which part of this code is calling for the game object to rotate? the 0, 0 of the person.transform.Translate? I'd liek to wrap my head aronud the code so that I will have less problems like this in the future ins$$anonymous$$d of forever finding help! sorry to be a bother.)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Look At while locked in a position 1 Answer

Rotate Object to face 2 axis? 1 Answer

LookAt() with X axis 1 Answer

Constrain LookAt to Local Axis 2 Answers

Make LookAt only rotate X Axis 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