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

Problem with camera tracking script (normalising vectors)

Discussion in 'Scripting' started by samnunn92, Sep 22, 2015.

  1. samnunn92

    samnunn92

    Joined:
    Oct 30, 2013
    Posts:
    4
    Hi all and thanks for reading.

    I am trying to implement a basic (or so I thought!) camera system to use in my game. Essentially I want the camera to follow a game object at a fixed distance (in this case a rolling ball) controlled by the player. I also want the player to be able to rotate the camera around the ball, and the movement vectors supplied by the player input to change accordingly (i.e. so forward is always in the direction the camera is facing).

    So far I have managed to get the camera following the ball, the rotation working and the player input working. I did this by using the transform.forward property of the camera and using that (and multiplications thereof) as the vectors in the transform.AddForce() method on the ball. This is sort of working okay, but the problem that I am having is that if I rotate the camera to be above the ball, the ball moves forward much slower, & vice versa if I position the camera immediately behind the ball it moves extremely fast. I understand that this is because the x (or z, depending on which way the camera is facing) components of the transform.forward vector are obviously lessened the closer the vector gets to the y-axis, but I want a smooth propulsion regardless of how steep the angle of the camera is - essentially, I only want the direction of the vector & not its magnitude. (I think...)

    I've tried normalising the transform.forward vector by dividing everything by its component and ending up with a unit vector, but that only works when the camera is positioned precisely in one direction, else the ball just propels equally by a unit of one in every direction.

    I'm really stumped at this point and am not sure whether I'm barking up the wrong tree wrt normalising the vectors. Would anyone be able to give me some pointers as to where I'm going wrong - either in my thinking or my coding! (Or, more likely, both!) I am quite new to coding and such and I know that this is probably a very nooby question but I'd really appreciate any help you'd be able to give me.

    Thanks for reading.
    Sam
     
  2. Michael-N3rkmind

    Michael-N3rkmind

    Joined:
    Aug 16, 2015
    Posts:
    24
    Hello Sam,

    I've made a function that rotates my InputVector with the camera's forward, i think this is what you need

    private Vector3 RotateWithView()
    {
    // Gets the direction the camera is facing , makes it 2D , apply the lenght of original move vector
    Vector3 dir = camTransform.TransformDirection(MoveVector);
    dir.Set(dir.x,0,dir.z);
    return dir.normalized * MoveVector.magnitude;
    }

    Simply run your input vector in there, and it should work :)
    ex :
    // Rotate the MoveVector with the camera direction
    MoveVector = RotateWithView();
     
  3. samnunn92

    samnunn92

    Joined:
    Oct 30, 2013
    Posts:
    4
    Hi Michael,

    Thankyou so much, that's fixed it! I didn't use your code exactly but took the clever parts out and put them in my existing script and it worked! (after some fiddling!) I can now crack on with making the rest of my game :) absolutely brilliant, can't thank you enough, been struggling with this all day!

    Cheers!
    Sam
     
  4. Michael-N3rkmind

    Michael-N3rkmind

    Joined:
    Aug 16, 2015
    Posts:
    24
    Glad to hear it Sam! If you need any more help you can contact me anytime! Information in the signature :)