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 /
avatar image
3
Question by Persona · Dec 09, 2010 at 01:02 PM · characterforwardlookrotationfacing

Look rotation facing vector zero

My character keeps looking at vector zero when I use the following script, rather than the direction it's moving in. Any suggestions?

if(charController.isGrounded == true) { // Calculate the movement direction (forward motion) //Move in the direction based on the negative of the horizon axis moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); transform.forward = moveDirection;

     if(Input.GetButtonDown("Jump")){
     ///Insert Jump here(Input.GetButton ("Jump")) 
     moveDirection.y = jumpSpeed;
     //audio.PlayOneShot(Jump);
 }

 }
 //Apply gravity every frame
 moveDirection.y -= gravity * Time.deltaTime;
 //Move using the direction and walkspeed multiplied by Time.deltaTime
 charController.Move(moveDirection * (Time.deltaTime * walkSpeed));

}

Comment
Add comment · Show 2
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 oliver-jones · Dec 09, 2010 at 01:06 PM 0
Share

$$anonymous$$aybe its not local?

avatar image Persona · Dec 09, 2010 at 06:45 PM 3
Share

You're gonna have to break it down more than that...I'm an Idiot here dude.

5 Replies

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

Answer by Loius · Dec 13, 2010 at 07:31 PM

You need to use instead of "transform.foward = moveDirection":

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

You can replace the .rotation = line with the one you have, but remember that you won't be able to directly control the up-vector as easily (LookRotation defaults to trying to align the object's +Y axis to the world's +Y axis)

Comment
Add comment · Show 15 · 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 Bravini · Dec 13, 2010 at 07:55 PM 0
Share

beated me by 20 $$anonymous$$ :]

avatar image Peter G · Dec 13, 2010 at 08:04 PM 0
Share

Actually, it's not read-only. It would throw an error if it was.

avatar image Loius · Dec 14, 2010 at 03:52 AM 0
Share

Well, wow, now I know something new. D:

avatar image Persona · Dec 14, 2010 at 02:58 PM 0
Share

No change. It acts the same as the transform forward. The character will move and rotate to the direction but just snap back to the forward when I let go.

avatar image Loius · Dec 15, 2010 at 03:53 AM 0
Share

Oh! It snaps back? You need to only apply the rotation change when the input is non-zero. Use "if ( moveDirection.sqr$$anonymous$$agnitude > 0 )"

Show more comments
avatar image
1

Answer by Proclyon · Dec 13, 2010 at 06:19 PM

You didn't tell him to look anywhere so he's not going to.

To do that you need to fiddle with the rotation property of the character.

It is stored in the transform.rotation.

For example rotation over Y axis 90 degrees would make your character face absolute right (not relative right).

If it's a 2D world like mario and you can only face absolute left and right Y -90 (or 270) or any increment or decrement of that with 360 (which is pointless but possible) will make your character face left or right.

In a 3D world however you need to do a lot more. So Please do tell what kind of world we're in.

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
1

Answer by Peter G · Dec 13, 2010 at 08:23 PM

I would have the character look at its own velocity. Then it always face the direction its moving. Like the CameraRelativeController.js in the Penelope tutorial.

function FaceMovementDirection() { var horizontalVelocity : Vector3 = charCharacter.velocity; horizontalVelocity.y = 0; // Ignore vertical movement

 // If moving significantly in a new direction, point that character in that direction
 if ( horizontalVelocity.magnitude > 0.1 )
     transform.forward = horizontalVelocity.normalized;

}

P.S. I haven't really used this tutorial for much other than ideas for future projects so I really don't know how well this works, just that I picked it out as logical, and I don't know why Unity would steer you wrong.

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 Design3.com · Dec 13, 2010 at 08:10 PM

1) Delete or comment out "transform.forward = moveDirection;" from the script

2) Add the Mouse Look script to your player game object from Component-->Camera Control-->Mouse Look

Vicenti's right; transform.forward is read-only and can be used to find out what direction an object is facing. By setting "moveDirection" and using CharacterController.Move() with that Vector3 value, you're doing all you need to.

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 Dinart Filho 1 · May 16, 2011 at 03:25 PM

Vicenti, you are a genious!

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

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

No one has followed this question yet.

Related Questions

Rotating to -forward goes upside down 1 Answer

Change characters direction using camera view 0 Answers

How would I make it so that my character moves only so fast 1 Answer

How to Rotate the Player In Direction of Movement? 4 Answers

Mixamo Character Movement 0 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