Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by JanniEgebo · Jan 18, 2016 at 05:41 PM · c#camerafollowballturn

How to make a camera follow a ball from behind? c#

Hello!

Im having some problems making the camera turn with the ball. https://youtu.be/6D337xSoQV4 in the YouTube link you can see the camera is following the ball but only show it from the same angle. I would like the camera to turn with the ball, so the backside of the ball is seen and it's possible for the player to see the road ahead.

the code for the camers:

using UnityEngine; using System.Collections;

public class FollowBall : MonoBehaviour {

 public GameObject player;
 private Vector3 offset;

 void Start () {
     offset = transform.position;
 }

 void LateUpdate () {
     transform.position = player.transform.position + offset;
 }

}

I dont know if it's needed but i have also added the code for the ball (PlayerController):

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 private Rigidbody rb;

 void Start (){
     rb = GetComponent<Rigidbody> ();
 }

 // Update is called once per frame
 void FixedUpdate () {
     float moveHorizontal = Input.GetAxis ("Horizontal");
     float moveVertical = Input.GetAxis ("Vertical");

     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

     rb.AddForce (movement);
 }

}

Hope someone have the answer! :)

Comment
Add comment · 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 gjf · Jan 16, 2016 at 08:36 PM 0
Share

please post actual code, NOT screenshots of it...

avatar image JanniEgebo gjf · Jan 16, 2016 at 10:15 PM 0
Share

Sorry, i have never tried using the forum before.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Castermaluney · May 17, 2016 at 05:49 PM

this should work:

 public GameObject player;
 
 private Vector3 _offset;
 
 void Start()
 {
    _offset = new Vector3 (0, y, z);    //just put the values that you want instead of y and z
 }
 
 void FixedUpdate()
 {
    Vector3 flatSpeed = player.rigidbody.velocity;
    flatSpeed.y = 0;
    //stores the flat velocity of your player so it can put the camera always behind it
 
    Quaternion wantedRotation;
 
    if (flatSpeed != Vector3.zero)
    {
        float targetAngle = Quaternion.LookRotation(flatSpeed).eulerAngles.y;
        wantedRotation = Quaternion.Euler(0, targetAngle, 0);
    }
 
    tranform.position = player.transform.position + (wantedRotation * _offset);
    transform.LookAt(player.transform);
 }
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
0

Answer by vintar · Jan 18, 2016 at 07:11 PM

Try something like this :

 public GameObject Player;
 Vector3 _offset;
 
 void Start()
 {
     _offset = new Vector3(0, -22, 10); // or whatever you need
 }
 
 void FixedUpdate()
 {
     transform.position = Player.transform.position + _offset;
     transform.rotation = Player.transform.rotation;
 }
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 Otaku_Riisu · May 23, 2016 at 05:23 PM

Try parenting the main camera to the ball. It seems simple, and it is, but works surprisingly well.

If that fails, follow this tutorial. It's in the middle of a series on RPG character controllers. The camera script works quite well, and you should have a very aesthetically pleasing result.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a Camera follow an object and then distance itself from the object when a button is held down? 1 Answer

Rotate camera when colliding 0 Answers

Camera Rotation Around Player 1 Answer

uNet hitboxes and movement. 0 Answers

When playing builded version of game the cameras only captures part of scene. 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