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 krotana · Aug 27, 2011 at 06:54 AM · movementtargetlook

Look at +1z of target and move to -1z of target? Almost there!

Hello guys, I am doing a small assignment for Uni where we have to make a small game.

I am doing a nokia style snake game in 3d where it follows gravity to platenauts (little planets)

At the moment I am in the script stage where I use simple shapes (spheres) to follow each other (and will grow and shorten depending on how much fruit it eats of if it hits walls)

After a whole day of looking around and testing its working well and I am happy! I still need to tweak it a little bit and I need to ask, how can I add +1 Z position to "look at" and -1 Z position to move at from target. If I can change this +1 or -1 numbers to look at and move at variables that would be more flexible. Anyone with good programming techniques should know this at a glace, but after one hour I cant find much.

This is the code I am using, they are following a empty game object that is positioned at -1 Z as a child of the spheres (the snake parts) but id like to eventually use a variable instead that indicates "target.position - (z.-1)" and look "target.position + (z.+1)"

This is the code I am using at the moment

 var target : Transform;
 var smooth = 5.0;
 var damping = 6.0; 
 
 
 function Update () {
     transform.position = Vector3.Lerp (
         transform.position, target.position,
         Time.deltaTime * smooth);
     
     var rotation = Quaternion.LookRotation((target.position + Vector3(0, 0, 1)) - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 }

ps the position has to be local from the object it self

Thanks in advance!

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 krotana · Aug 27, 2011 at 06:56 AM 0
Share

also, it would be fantastic to have control of the -1Z (the move to position) and +1Z (Look at position) as variables

Thanks a bunch!

2 Replies

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

Answer by krotana · Aug 28, 2011 at 08:31 AM

So I got the code working, but its not idea only because I am using multiples to use with target.forward instead of variables to change the vector3, and I also need to clap the rotations of every axis.

     var target : Transform;
 var smooth = 5.0;
 var damping = 6.0; 
 var lookatDistanceZ: float = 2;
 var moveatDistanceZ:float = 4;
 var lookatDistance:Vector3 = Vector3(0,0,lookatDistanceZ);
 var moveatDistance:Vector3 = Vector3(0,0,moveatDistanceZ);
 
 
 
 function Update () {
     transform.position = Vector3.Lerp (
         transform.position, (target.position - (target.forward*moveatDistanceZ)),
         Time.deltaTime * smooth);
     
     var rotation = Quaternion.LookRotation((target.position + (target.forward*lookatDistanceZ)) - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 }
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 DaveA · Aug 27, 2011 at 06:57 AM

Not sure I followed all that, but it sounds like you may need the transform.forward member.

Comment
Add comment · Show 4 · 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 krotana · Aug 27, 2011 at 07:15 AM 0
Share

http://krotana.deviantart.com/gallery/?catpath=scraps&offset=0#/d47rso1

This is what more or less I am after.

This code is applied to each sphere that follows just behind the spheres (and looks at) the sphere at front.

I need to ADD or $$anonymous$$INUS the Z position of target. How you do that? Its so simple but so annoying I am not able to figure out. Do I use target.position + Vector3(0, 0, 1) or something like that? it does not seem to work

avatar image Rennat · Aug 27, 2011 at 07:23 AM 0
Share

You can use target.position + target.forward and target.position - target.forward to get the points 1 unit in front of and behind an object.

avatar image krotana · Aug 28, 2011 at 05:21 AM 0
Share

ok, thanks Rennat, will try that!

avatar image krotana · Aug 28, 2011 at 05:36 AM 0
Share

Its working, thanks Rennat! I will post up the answer when I can clamp the rotation values and use the targer.forward as variable ins$$anonymous$$d!

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

Prevent random movement allowing movement through obstacles 1 Answer

Movement along X and Z axis... 2 Answers

Determining Look Rotation As > A Number? 1 Answer

Want to apply "height" to my DeadSpace Camera --> Physics.Linecast gives me trouble 1 Answer

Changing Head Look Controller Target 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