Search Unity

3rd Person Mouse Controlled Camera

Discussion in 'Scripting' started by TwentySicks, Mar 16, 2013.

  1. TwentySicks

    TwentySicks

    Joined:
    Sep 6, 2012
    Posts:
    51
    Hello guys.

    I'm trying to create a kind of free-floating camera, which orbits a character, making the perspective 3rd person.

    The point is to make the player character rotate and look where the mouse cursor is on the screen.

    So far I have two scripts, and it works a little bit. The only problem for me is that the camera view keeps sliding across the screen and S***, a player would never be able to control it. Right now, I have the camera being a child of the character model, sitting inside his head area, I dont know if that might be causing problems.

    How can i fix this?

    Here is the code which is placed on my camera.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraScript : MonoBehaviour
    5. {
    6.     // Use this for initialization
    7.     void Start ()
    8.     {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update ()
    14.     {
    15.         RaycastHit hit;
    16.         if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
    17.         {
    18.             gameObject.transform.LookAt(hit.point);
    19.         }
    20.     }
    21. }
    And this code is placed on my character prefab

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement : MonoBehaviour
    5. {
    6.  
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update ()
    15.     {
    16.         if(Input.GetKey(KeyCode.W))
    17.         {
    18.             gameObject.transform.Translate(Vector3.forward);
    19.         }
    20.         if(Input.GetKey(KeyCode.A))
    21.         {
    22.             gameObject.transform.Translate(Vector3.left);
    23.         }
    24.         if(Input.GetKey(KeyCode.S))
    25.         {
    26.             gameObject.transform.Translate(Vector3.back);
    27.         }
    28.         if(Input.GetKey(KeyCode.D))
    29.         {
    30.             gameObject.transform.Translate(Vector3.right);
    31.         }
    32.     }
    33. }
    Also, for reference, I am trying to re-create a camera which acts like the camera from Mount&Blade: Warband;

    http://www.youtube.com/watch?v=yqgjw4EvbHo
     
  2. TwentySicks

    TwentySicks

    Joined:
    Sep 6, 2012
    Posts:
    51
    Bump. Anyone got some input or directions?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    why do you have "out" in the raycast call? I've not seen that before...