• 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 Chocolade · May 22, 2017 at 03:27 PM · c#scripting problemscript.

How can i make the camera to lookat the next target and then rotate and move to the next target ?

The first script is just making the camera to move over the terrain this script i'm not changing anything. The second script is PatrolData with that i feed the first script with data. The last script is the LookAt that should rotate the camera just a bit before moving to the next target(waypoint).

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FlyToOverTerrain : MonoBehaviour
 {
     public Transform target;
     public float desiredHeight = 10f;
 
     public float flightSmoothTime = 10f;
     public float maxFlightspeed = 10f;
     public float flightAcceleration = 1f;
 
     public float levelingSmoothTime = 0.5f;
     public float maxLevelingSpeed = 10000f;
     public float levelingAcceleration = 2f;
     
     private Vector3 flightVelocity = Vector3.zero;
     private float heightVelocity = 0f;
 
     private void LateUpdate()
     {
         Vector3 position = transform.position;
         float currentHeight = position.y;
         if ((bool)target && flightAcceleration > float.Epsilon)
         {
             position = Vector3.SmoothDamp(position, target.position, ref flightVelocity, flightSmoothTime / flightAcceleration, maxFlightspeed, flightAcceleration * Time.deltaTime);
         }
 
         if (levelingAcceleration > float.Epsilon)
         {
             float targetHeight = Terrain.activeTerrain.SampleHeight(position) + desiredHeight;
 
             position.y = Mathf.SmoothDamp(currentHeight, targetHeight, ref heightVelocity, levelingSmoothTime / levelingAcceleration, maxLevelingSpeed, levelingAcceleration * Time.deltaTime);
         }
 
         transform.position = position;
     }
 }
 

Then the data script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class PatrolData
 {
     public Transform target = null;
     public float minDistance = 5f;
     public float lingerDuration = 5f;
 
     public float desiredHeight = 10f;
 
     public float flightSmoothTime = 10f;
     public float maxFlightspeed = 10f;
     public float flightAcceleration = 1f;
 
     public float levelingSmoothTime = 0.5f;
     public float maxLevelingSpeed = 10000f;
     public float levelingAcceleration = 2f;
 }
 
 public class PatrolOverTerrain : MonoBehaviour
 {
     public FlyToOverTerrain flyOverTerrain;
     public enum PatrolMode { Clamp, Wrap, PingPong };
     public PatrolData[] patrolPoints;
     public PatrolMode mode = PatrolMode.Wrap;
 
     private int iterator = 0;
     private int index = 0;
     private float lingerDuration = 0f;
 
     public Vector3 distanceFromTarget;
 
     private void OnEnable()
     {
         if (patrolPoints.Length > 0)
         {
             lingerDuration = patrolPoints[index].lingerDuration;
         }
     }
 
     private void Update()
     {
         int length = patrolPoints.Length;
         if (!flyOverTerrain) return;
         if (patrolPoints.Length < 1) return;
         if (index < 0) return;
 
         var patrol = patrolPoints[index];
         if (lingerDuration <= 0)
         {
             iterator++;
             switch (mode)
             {
                 case PatrolMode.Clamp:
                     index = (iterator >= length) ? -1 : iterator;
                     break;
                 case PatrolMode.Wrap:
                     iterator = Modulus(iterator, length);
                     index = iterator;
                     break;
                 case PatrolMode.PingPong:
                     iterator = Modulus(iterator, length * 2);
                     index = length - Mathf.Abs(length - iterator);
                     break;
             }
             if (index < 0) return;
 
             patrol = patrolPoints[index];
 
             flyOverTerrain.target = patrol.target;
             flyOverTerrain.desiredHeight = patrol.desiredHeight;
             flyOverTerrain.flightSmoothTime = patrol.flightSmoothTime;
             flyOverTerrain.maxFlightspeed = patrol.maxFlightspeed;
             flyOverTerrain.flightAcceleration = patrol.flightAcceleration;
             flyOverTerrain.levelingSmoothTime = patrol.levelingSmoothTime;
             flyOverTerrain.maxLevelingSpeed = patrol.maxLevelingSpeed;
             flyOverTerrain.levelingAcceleration = patrol.levelingAcceleration;
 
             lingerDuration = patrolPoints[index].lingerDuration;
         }
 
 
         Vector3 targetOffset = Vector3.zero;
         if ((bool)patrol.target)
         {
             targetOffset = transform.position - patrol.target.position;
         }
 
         float sqrDistance = patrol.minDistance * patrol.minDistance;
         if (targetOffset.sqrMagnitude <= sqrDistance)
         {
             flyOverTerrain.target = null;
             lingerDuration -= Time.deltaTime;
         }
         else
         {
             flyOverTerrain.target = patrol.target;
         }
 
         distanceFromTarget = transform.position - patrol.target.position;
     }
 
 
     private int Modulus(int baseNumber, int modulus)
     {
         return (modulus == 0) ? baseNumber : baseNumber - modulus * (int)Mathf.Floor(baseNumber / (float)modulus);
     }
 }
 

And last the LookAt script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LookAtCamera : MonoBehaviour {
 
     //values that will be set in the Inspector
     public Transform target;
     public float RotationSpeed;
 
     //values for internal use
     private Quaternion _lookRotation;
     private Vector3 _direction;
 
     // Update is called once per frame
     void Update()
     {
         //find the vector pointing from our position to the target
         _direction = (target.position - transform.position).normalized;
 
         //create the rotation we need to be in to look at the target
         _lookRotation = Quaternion.LookRotation(_direction);
 
         //rotate us over time according to speed until we are in the required rotation
         transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
     }
 }
 

Now all scripts are attached to the Main Camera. Until now it was working fine the camera moved between the targets. Now what i want to do is when the camera stop near each target just a bit before the camera start moving to the next target make a rotation to be facing to the target it's going to be moving to.

The problem is i don't know how to make it wait and how much and when to start the rotation in the LookAtCamera script.

Now what it does when running the game it's start rotating right away to the next target(in inspector i dragged for testing the second target).

My problem is how to work with the LookAtCamera script.

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

333 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 can i check and fire an event when the user look at specific object ? 0 Answers

How can i List objects by name but also in small text or big text or any kind ? 1 Answer

Why the speed parameter in Animator change the animation speed only for door open but when the door is closing the speed still very fast ? 1 Answer

How can i make an entrance and exit in this maze ? 1 Answer

How can I animate linerenderer lines over time ? 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