Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

can anyone help with vall follow camera

Discussion in 'Scripting' started by melonhead, Feb 6, 2017.

  1. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    i am pulling my hair with this, i have a rolling ball that uses the arrow keys for movement, i need a camera that rotates around the ball when the ball turns so that the camera is always behind the ball, the camera needs to bank to the side when the ball turns left or right then slowly rotate back when the ball starts rolling straight again.

    i have exhausted all the scripts i can find and have tried to adjust some of them but to no avail, it just will not work.

    help appreciated
     
  2. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    can anybody help me with this ?
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    to do this, you will need to write a follow script, which sets the camera at a fixed distance based on the balls velocity.

    say your ball moves at 1,0.5, 1. First, remove the y, so you get 1,0,1, now reverse it to get -1, 0, -1. Normalize to make it a unit of 1. Now, take the normalized vector times the follow distance. Add a raw y of 1, so you get like -7.7, 1, -7.7 (if following at 10 units) add the balls position. This now gives you the position of the camera. (or where you want the camera to be) Now, simply lerp that position over time (camera.position = Math.Lerp(camera.position, newPosition, 5 * Time.deltaTime). This will move the camera over time so it doesn't seem so jerky.

    OK, now for the last good bit. During this calculation, you will need to figure out the orientation of the motion. For simplicity's sake, try something like this:

    Code (csharp):
    1. var cameraTarget = (rigidbody.velocity * new Vector3(-1, 0, -1)) .normalized * 10 + transform.position;
    2. var currentPosition = camera.position;
    3. var position = camera.position;
    4. position.y = transform.y;
    5. camera.position = Vector3.Lerp(position, cameraTarget, 5, * Time.deltaTime);
    6. camera.LookAt(transform.position);
    7.  
    8. // now, get the input
    9. var input = new Vector3(-Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    10. var force = camera.TransformDirection(input) * 100;
    11. rigidbody.AddForce(force);
    12.  
    13. // now, with the force applied, move the camera up.
    14. camera.position = camera.position + Vector3.up;
    15. camera.LookAt(transform);
    Of course its loose, and It kind of follows what I remember of moving stuff around, but its a start. ;)
     
    Last edited: Feb 9, 2017
  4. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    is that in javascript as i need c# please
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I believe it's both. C# and javascript. If you are asking me to write all of your code for you, no, you need to learn how to use, and read code and translate it for your use. All languages are effectively the same, as long as you know how to translate them. ;)
     
    eses likes this.
  6. smacbride

    smacbride

    Joined:
    Jan 10, 2015
    Posts:
    50
    Unity Standard Assets example project has some good camera scripts and techniques that will do what you want.