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
1
Question by Asuperventure · May 19, 2011 at 08:05 PM · rotateaxislookatfollow

Another way to follow an object?

Currently I am using

 if ( Vector3.Distance( leader.position, transform.position ) >= distance) {
     transform.LookAt(leader);
     controller.SimpleMove(forward * speed );

But I am curious as to another way to get a npc to follow the 'leader'. This is fine in theory, but the npc's z points towards the leader unrealisticly. Is there a way to get the npc to face the player while keeping its z normal?

Right now I am restricting its z bu using

transform.rotation.z = 0;

I don't want to have such a restriction on it though. In the future I want to add functions to allow the npc to rotate to the ground beneath it, and restrictions like that will be a huge problem. I basically need a new way to get it to follow something more naturally.

Thanks for any help

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 Asuperventure · May 19, 2011 at 08:09 PM 0
Share

Oh, and I am using a character controller, if it helps. no rigidbodies

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ckfinite · May 19, 2011 at 08:10 PM

What I have done is the past is create a Quaternion that points the character in the right direction, then add the other Quaternion that defines the slope.

Edit: Quaternions are actually really simple to use. Basically you use something like Quaternion.LookRotation to make one, then you make another, then just use the * operator to add them.

 Quaternion.LookRotation(transform.forward, Vector3.up) * Quaternion.RotateTowards(something)

The docs are pretty good for this.

Comment
Add comment · Show 11 · 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 Asuperventure · May 20, 2011 at 03:05 AM 0
Share

Thanks for the start off, but I don't why I am getting error messages. I have tried the original like so

Quaternion.LookRotation(transform.forward, Vector3.up) * Quaternion.RotateTowards(leader.position);

and then tried

Quaternion.LookRotation(transform.forward, Vector3.up) * Vector3.RotateTowards(leader.position);

but I get an error message

'RotateTowards' is not a member of 'UnityEngine.Quaternion'

what's up?

avatar image ckfinite · May 20, 2011 at 03:15 AM 0
Share

Sorry, that was a lousy example, sine it does not work and is useless. Ally you really need at the moment is Quaternion.LookRotation(leader.position, Vector3.up), to look at the "leader". If you want to add the slope, just change Vector3.up to the direction vector of the slope.

avatar image ckfinite · May 20, 2011 at 03:15 AM 0
Share

Sorry, that was a lousy example, sine it does not work and is useless. Ally you really need at the moment is Quaternion.LookRotation(leader.position, Vector3.up), to look at the "leader". If you want to add the slope, just change Vector3.up to the direction vector of the slope.

avatar image Asuperventure · May 20, 2011 at 03:33 AM 0
Share

No example is lousy here. I don't even know where to go with quaternions. Anyway, the new example makes the npc walk forward on their world z infinitely for some reason. They don't rotate in the least. How exactly does Quaternion.LookRotation(leader.position, Vector3.up) differ from transform lookAt?

avatar image ckfinite · May 20, 2011 at 03:36 AM 0
Share

Sorry, messed up again. You need the direction, not the absolute position. Try Quaternion.LookRotation(leader.position- transform.position, Vector3.up)

Show more comments
avatar image
0

Answer by Asuperventure · May 20, 2011 at 04:19 AM

 function Update () {
 
 transform.rotation.z = 0;
 
 
 
 var hit : RaycastHit;
 var castPos = Vector3(transform.position.x,transform.position.y-.25,transform.position.z);
 if (Physics.Raycast (castPos, -transform.up, hit)) {
     transform.rotation.x = Quaternion.FromToRotation (Vector3.up, hit.normal).x;
 }
 }


This makes the character extremely buggy and trippy. I think something is screwy in its update function?

Comment
Add comment · Show 9 · 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 ckfinite · May 20, 2011 at 01:24 PM 0
Share

The reason why it is so weird is that you are only setting the x rotation of the transform, not the entire thing, and Quaternions don't really like that. Change the line transform.rotation.x = Quaternion.FromToRotation (Vector3.up, hit.normal).x; to transform.rotation = Quaternion.FromToRotation (leader.position-transform.position, hit.normal);

avatar image Asuperventure · May 20, 2011 at 06:43 PM 0
Share

:)... You are right sir, I did that to the code. I was messing with it because without it, the y rotation is completely restricted north in the world. Can't turn at all. Do you know how to release the y rotation? I will stop bugging you after this! Thanks a bunch for all your help- you will be getting super up votes from me!

avatar image ckfinite · May 20, 2011 at 08:30 PM 0
Share

The Character Controller actually ignores the Y component of the vector input, so it isn't a problem. Anyway, it would not have really worked since Quaternions don't work like Eulers.

Also, did you make all the changes I showed? The Y rotation is controlled by the first parameter, so it might be the problem.

avatar image Asuperventure · May 20, 2011 at 09:21 PM 0
Share

Yes, I took of the .x's from that line of code, and it locked rotation y rotation again. Do you mean castpos is the problem, or the if statement?

avatar image ckfinite · May 20, 2011 at 11:07 PM 0
Share

Does your code look like this: transform.rotation = Quaternion.FromToRotation (leader.position-transform.position, hit.normal);

or like this: transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);

If number 2, then make it look like number one. The reason why is the first parameter controls the direction the rotation is facing, so you original code will have it point up only.

Show more comments

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

2 People are following this question.

avatar image avatar image

Related Questions

Camera rotation around player while following. 6 Answers

Moving while facing the camera 0 Answers

Moving a model relative to one axis 1 Answer

Rotation in 8 way direction 2 Answers

How can I rotate towards/look at a specific axis of a collided object? 2 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