• 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 /
  • Help Room /
avatar image
Question by Wimmydittle · Nov 20, 2015 at 11:31 AM · cameracamera-movementcamera rotatecamera followsmoothfollow

C# Smooth Follow Space Ship

Hello. I have t$$anonymous$$s space s$$anonymous$$p that has a camera as a c$$anonymous$$ld of it. Before, the camera would just stay be$$anonymous$$nd the space s$$anonymous$$p in the position where I placed it as a c$$anonymous$$ld. Now, I want to have the camera follow the s$$anonymous$$p smoothly, and I want the s$$anonymous$$p to turn smoothly to make it look more legit. The problem is, when I use scripts for smooth following from the standard asset package and other questions, the camera does not follow the s$$anonymous$$p right. Here is the script for the s$$anonymous$$p. There is no script for the camera.

 using UnityEngine;
 using System.Collections;
 
 public class Player1 : MonoBehaviour
 {
     public float yawSpeed;
     public float rollSpeed;
     public float pitchSpeed; 
     public float BoostSpeed;
     public float BrakeSpeed;
     public float S$$anonymous$$pSpeed;
     public Rigidbody projectile;
     public float ProjSpeed;
     public Transform Launch;
     public float DefaultSpeed;
 
 
 
 
     void Start () {
         S$$anonymous$$pSpeed = DefaultSpeed;
         Screen.lockCursor = true;
     }
         
 
     void Update ()
     {
         
         transform.Translate(Vector3.forward * S$$anonymous$$pSpeed * Time.deltaTime);
 
             if(Input.GetKey(KeyCode.W)){ //accelerate
                 S$$anonymous$$pSpeed = BoostSpeed;}
 
                 else if(Input.GetKey(KeyCode.S)) {//decelerate
                     S$$anonymous$$pSpeed = BrakeSpeed;}
                     
                     else {
                         S$$anonymous$$pSpeed = DefaultSpeed;}
         
             if (Input.GetAxis("MouseX") < 0) //yaw left
                 transform.Rotate(Vector3.up, -yawSpeed * Time.deltaTime);
         
             if (Input.GetAxis("MouseX") > 0) //yaw right
                 transform.Rotate(Vector3.up, yawSpeed * Time.deltaTime);
         
             if (Input.GetAxis("MouseY") < 0)//pitch down
                 transform.Rotate(Vector3.right, -pitchSpeed * Time.deltaTime);
         
             if (Input.GetAxis("MouseY") > 0)  //pitch up
                 transform.Rotate(Vector3.right, pitchSpeed * Time.deltaTime);
         
             /*if(Input.GetKey(KeyCode.E)) //roll right
                 transform.Rotate(Vector3.forward, -rollSpeed * Time.deltaTime);
         
             if(Input.GetKey(KeyCode.Q)) //roll left
                 transform.Rotate(Vector3.forward, rollSpeed * Time.deltaTime);*/
 
             if(Input.GetKeyDown(KeyCode.Space)){    //Shooting
                 Rigidbody InstantPro = Instantiate(projectile, Launch.position, Launch.rotation) as Rigidbody;
                 InstantPro.AddForce(transform.forward * ProjSpeed  * Time.deltaTime);
         }
 
 
 
     }
 }
Comment

People who like this

0 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 Dinosaurs · Nov 20, 2015 at 01:35 PM 0
Share

This question is very vague. What does "the camera does not follow the ship right." mean? If there is no script for the camera, how should it be following? Please be more specific when submitting questions to Unity Answers, and review the FAQ suggestions on how to write good questions http://answers.unity3d.com/page/faq.html

avatar image Wimmydittle · Nov 22, 2015 at 08:50 PM 0
Share

It's difficult to explain, so I didn't want to further confuse people. The script from standard assets just makes the camera flip around, and it doesn't always return to exactly the right target spot.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by degraffenried · Jul 31, 2016 at 07:34 PM

I was looking for the same answer, and used a smooth follow script that was ported to C#, and modified it for 3D space. The trick is to use quaternions. I'm still getting used to the quaternion libraries that Unity uses, but in the past I did transformations directly using the matrices. Fortunately Unity has those libraries available. So, here is my code, just tweak it a bit to make it perform the way you need.

  using UnityEngine;
  using System.Collections;
  
  [AddComponentMenu("Camera-Control/Smooth Follow 3D CSharp")]
  
  public class SmoothFollow3D : MonoBehaviour
  {
      
  
      // The target we are following
      public Transform target;
      // The distance in the x-z plane to the target
      public float distance = 10.0f;
      // the height we want the camera to be above the target
      public float height = 5.0f;
      // How much we 
      public float heightDamping = 2.0f;
      public float rotationDamping = 3.0f;
  
      void  LateUpdate ()
      {
          // Early out if we don't have a target
          if (!target)
              return;
      
         Vector3 followpos = new Vector3(0.0f, height, -distance);
         Quaternion lookrotation = Quaternion.identity;
    
         lookrotation.eulerAngles = new Vector3(30.0f, 0.0f, 0.0f);
 
         Matrix4x4 m1  = Matrix4x4.TRS(target.position, target.rotation, Vector3.one);
         Matrix4x4 m2  = Matrix4x4.TRS(followpos, lookrotation, Vector3.one);
         Matrix4x4 combined  = m1 * m2;
         
         // THE WAY TO GET POSITION AND ROTATION FROM A MATRIX4X4:
         Vector3 position = combined.GetColumn(3);
         
         Quaternion rotation = Quaternion.LookRotation(
         combined.GetColumn(2),
         combined.GetColumn(1)
         );
 
 
          Quaternion wantedRotation = rotation;
          Quaternion currentRotation = transform.rotation;
 
          Vector3 wantedPosition = position;
          Vector3 currentPosition = transform.position;
       
          currentRotation = Quaternion.Lerp(currentRotation, wantedRotation, rotationDamping * Time.deltaTime);
          currentPosition = Vector3.Lerp(currentPosition, wantedPosition, heightDamping * Time.deltaTime);
      
         transform.localRotation = currentRotation;
         transform.localPosition = currentPosition;
          
         
      }
  }

Comment

People who like this

0 Show 0 · 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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

I'm too dumb to make a Tekken-like camera, can someone help me with it? 1 Answer

CineMachine Camera Movement is inverse 1 Answer

Spyro Like Camera Follow 0 Answers

When attaching my custom camera script camera shakes when player starts to move fast. 1 Answer

Making the camera rotation in line with the ball 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