Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed May 10, 2013 at 07:42 AM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by DWallace3099 · May 09, 2013 at 11:20 PM · rotatebeginnerlookat

make my object face the direction of movement?

I have these two classes here, given to me as part of a project:

 using UnityEngine;
 using System.Collections;
 
 public class Ball: MonoBehaviour {
   public float moveSpeed = 30;
   
   void Update () { 
         
     //var dir: Vector3 = Vector3.zero;
         
     CharacterController controller = GetComponent<CharacterController>();
         
     Vector3 moveDirection = Vector3.zero;
 
     moveDirection += Camera.main.transform.up * Input.GetAxis("Vertical");
     moveDirection += Camera.main.transform.right * Input.GetAxis("Horizontal");
     moveDirection.z = 0;
     
     controller.Move(moveDirection * moveSpeed * Time.deltaTime);
   }
 }


and this one

 using UnityEngine;
 using System.Collections;
 
 public class GamePad : MonoBehaviour {
     public float speed = 40;
     // Update is called once per frame
     void Update () {
         transform.Translate (Input.GetAxis("Horizontal")*Time.deltaTime*speed,
             Input.GetAxis ("Vertical")*Time.deltaTime*speed,
             0);
     }
 }

these two I'm almost certain control movement along the X and Y axis. I have a sphere slightly streched and I want it to rotate it to the direction it is moving. Any ideas? Thanks!

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

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by robertbu · May 10, 2013 at 01:08 AM

In order to make your object face the direction you need to:

  • Create your stretch sphere on the Z axis (i.e. the head and tail need to be on the Z axis). If you are creating it from a standard sphere, just increase the size of the Z scale, or decrease the size of the x and y scale. In Unity, positive 'Z' is forward (which is where you want the the head).

  • Add the two lines of code at the bottom of this post at line 18 in the first script.

Note if you want a smoother rotation, take a look at the answer I posted for your other question (here)...the few lines posted by @runevision just after the bold 'Edit' are an implementation of a smoothed rotation that build on the two lines of code below.

  if (moveDirection != Vector3.zero)
    transform.rotation = Quaternion.LookRotation(moveDirection);

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 DWallace3099 · May 10, 2013 at 07:33 PM 0
Share

this is the code after adding your two lines

 using UnityEngine;
 using System.Collections;
 
 public class Ball: $$anonymous$$onoBehaviour {
   public float moveSpeed = 30;
   
   void Update () { 
         
         CharacterController controller = GetComponent<CharacterController>();
         
         Vector3 moveDirection = Vector3.zero;
 
         moveDirection += Camera.main.transform.up * Input.GetAxis("Vertical");
         moveDirection += Camera.main.transform.right * Input.GetAxis("Horizontal");
         moveDirection.z = 0;
         if (moveDirection.sqr$$anonymous$$agnitude != Vector3.zero)
         {
             transform.rotation = Quaternion.LookRotation(moveDirection);
         }
         
         controller.$$anonymous$$ove(moveDirection * moveSpeed * Time.deltaTime);
   }
 }

However, I'm getting an error that says on the line of the if(moveDirection...) statement you gave me that says Assets/Ball.cs(16,27): error CS0019: Operator !=' cannot be applied to operands of type float' and `UnityEngine.Vector3'

avatar image robertbu · May 11, 2013 at 04:47 AM 0
Share

I'm sorry, I changed that at the last moment. You can do it one of two ways. You can say:

 if (moveDirection != Vector3.zero)

or

 if (moveDirection.sqr$$anonymous$$agnitude < some_small_value)

I think the first statement is good enough and likely better, but when I ran a quick test of the concept, I used the second version. I

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Lock rotation axis? 4 Answers

Rotating Toward an Object at a Constant Speed 2 Answers

hoe to rotate a AI as a animation 1 Answer

Rotate to a certain position 2 Answers

Merge a rotation(vector3) to a lookAt(quaternion) ? 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