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
1
Question by antiReady · Sep 18, 2013 at 10:47 PM · movementmove an objectsmoothfollowvector3.slerp

Make Object smoothly follow player if out of near range

I just wrote a script that lets an object follow the player at all times if the player is a certain range away from the object (to be used as a camera target object). It basically works just the way I want it to work but the movement is a bit jittery and I have no idea why. I tested a bunch of different ways to do it and even though this one works I'm sure there is a much better way to do it... I'm fairly new to unity so I wouldn't actually know -.-

Anyways, the script moves the gameobject towards the target(player) when the player is farther than 0.75 Units aways ... so effectively the camera only starts to move when the player moves at least that amount in any direction.

Heres what I've got so far:

 using UnityEngine;
 using System.Collections;
 
 public class CameraTargetFollowPlayer : MonoBehaviour {
     
     public float targetDistance = 0.5F;
     public float smoothTime = 10;
     public Transform target;
 
     private float distance = 1F;
 
     Transform temptarget = null;
 
     void LateUpdate () {
     
         transform.LookAt(target);
         
         distance = Vector3.Distance( transform.position, target.position) ;
 
 
         // reset if its too far away
         if (distance > 7) {
             transform.position =  target.position-transform.forward;
         } else {
             
             if (distance > targetDistance) {
                                 
                 transform.position = Vector3.Slerp( transform.position, new Vector3 (
                     target.position.x-(transform.forward.x*targetDistance),
                     target.position.y,
                     target.position.z-(transform.forward.z*targetDistance)
                     ),
                     Time.deltaTime * smoothTime
                 );
                 
             } else {
                 // if distance isnt big enough to move the target we still need to adjust the y height every frame
                 transform.position = new Vector3(transform.position.x, target.position.y, transform.position.z);
                 
             }
             
         }
 
         
     }
     
     
 }
 
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

· Add your reply
  • Sort: 
avatar image
0

Answer by antiReady · Sep 19, 2013 at 06:31 PM

Alright ... it kind of solved itself I think. The problem isn't within this follow script but my camera movement code .. :)

So yeah .. if anyone ever needs some script for smoothly following something at a min distance, here it is :)

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

16 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 avatar image avatar image avatar image

Related Questions

How to drag a game object with a mouse (along x axis)? 1 Answer

Moving an object on the X axis corresponding to mouse movment 0 Answers

Moving objects problem 1 Answer

Character not moving... 2 Answers

How can i make it so that the object doesnt instantly go to top speed. Here is my code 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