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 /
  • Help Room /
avatar image
1
Question by Propellent · Nov 07, 2016 at 05:22 PM · rotationthird-person-camerabehind

Camera Follow Behind A Rolling Ball

I have a player that consists of a standard Unity 3D sphere, with a rigidbody, and a robot/player that sits on top of that ball. The ball rolls and the player follows. The player on top rotates left or right and the ball moves in the direction that the player is facing. My problem is I don't know how to go about creating a camera that will stay behind the player above the ball, not jitter, and rotate with the player. I've tried numerous scripts with my own modifications and nothing seems to work. Whenever I try to lerp a rotation it just spins back and forth between 0 and 90 degrees on the y axis.

If it helps this is the movement script for my ball and the rotation script for the player on top.

moving.cs

 using UnityEngine;
 using System.Collections;
 
 public class moving : MonoBehaviour {
 
     private Rigidbody pRigid;
     private float distToGround;
 
     public Transform pivot;
     public GameObject target;
     public float speed = 10;
     public float jumpForce = 10;
     public float SLOPE_MULTIPLIER;
 
 
     void Start()
     {
         pRigid = GetComponent<Rigidbody>();
 
         distToGround = GetComponent<Collider>().bounds.extents.y;
     }
 
     void FixedUpdate()
     {
         int moveV = (int)Input.GetAxisRaw("Vertical");
         int moveH = (int)Input.GetAxisRaw("Horizontal");
 
         if (Input.GetAxis("Jump") > 0 && isGrounded())
         {
             pRigid.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
 
             target.GetComponent<BodyController>().anim.SetTrigger("Jump");
         }
 
         RaycastHit hit;
         if (Physics.Raycast(pivot.position, transform.forward, out hit, 1.0f))
         {
             if (Vector3.Dot(Vector3.up, hit.normal) > -0.5)
             {
                 if (isGrounded())
                 {
                     if (moveV < 0)
                         pRigid.AddForce(target.transform.forward * speed * SLOPE_MULTIPLIER);
                     else if (moveV > 0)
                         pRigid.AddForce(-target.transform.forward * speed * SLOPE_MULTIPLIER);
                     else if (moveH < 0)
                         pRigid.AddForce(target.transform.right * speed * SLOPE_MULTIPLIER);
                     else if (moveH > 0)
                         pRigid.AddForce(-target.transform.right * speed * SLOPE_MULTIPLIER);
                 }
             }
         }
 
         if (isGrounded())
         {
             if (moveV < 0)
                 pRigid.AddForce(target.transform.forward * speed);
             else if (moveV > 0)
                 pRigid.AddForce(-target.transform.forward * speed);
             else if (moveH < 0)
                 pRigid.AddForce(target.transform.right * speed);
             else if (moveH > 0)
                 pRigid.AddForce(-target.transform.right * speed);
         }
     }
 
     public bool isGrounded()
     {
         return Physics.Raycast(transform.position, -Vector3.up, distToGround + .05f);
     }
 }

PlayerRotation.cs

 using UnityEngine;
 using System.Collections;
 
 public class PlayerRotation : MonoBehaviour
 {
     public Transform pivot;
 
     void LateUpdate()
     {
         if (Input.GetAxisRaw("Turn") != 0)
         {
             transform.RotateAround(pivot.transform.position, Vector3.up, Input.GetAxisRaw("Turn"));
         }
     }
 }

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 Golauszko · Nov 07, 2016 at 07:57 PM

So, to follow it will be:

     public GameObject player
     private Vector3 offset;
 
     // Use this for initialization
     void Start () {
         offset = transform.position - player.transform.position;
     
     }
         
     void LateUpdate () {
         transform.position = player.transform.position + offset;    
     }


And then try to set rotation of camera same as the player's.

Comment
Add comment · Show 1 · 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 Propellent · Nov 07, 2016 at 08:24 PM 0
Share

I need the camera to stay behind the character, even when it turns. This is what I've got so far for a camera script even though it's broken:

  using UnityEngine;
  using System.Collections;
  
  public class TPcam : $$anonymous$$onoBehaviour
  {
      public Transform target;
      public float smoothing;
      public float turnSpeed;
  
      private Vector3 offset;
  
      void Start()
      {
          offset = transform.position - target.position;
      }
  
      void LateUpdate()
      {
         transform.rotation = Quaternion.Lerp(transform.rotation, target.rotation, Time.deltaTime);
  
          transform.position = Vector3.Lerp(transform.position, target.position + offset, Time.deltaTime * smoothing);
          transform.Rotate(target.up, turnSpeed * Input.GetAxis("Turn"));
  
          transform.LookAt(target);
      }
  }

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

90 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

Related Questions

How do i rotate third person camera around my character while holding down right mouse button? 0 Answers

Third Person Camera - Issues to clamp rotation 0 Answers

No Particle Rotation When Setting rotation3D from C# 0 Answers

I'm trying to use Physics.Raycast to rotate the player to face the mouse cursor but it's not working like it should 0 Answers

Ambisonic rotation not working,Multichannel Ambisonics files not rotating 0 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