Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink
Share
Go down
Member [Lv. 1]
Member [Lv. 1]
Posts : 2
Points : 1326
Rep : 0
Join date : 2014-12-09
View user profile

Basing player movement off camera rotation when not parent/child without affecting player rotation

on Tue Dec 09, 2014 4:50 pm
I'm making a top down 3rd person game, I have a CamFocus object with the maincam as child, independent of the characters because i want to be able to move the camera independent of character movement

player rotation is focused on the mouse point, where the mouse is on the screen that's where the player is rotated to face. movement is handled through standard character controller movement based on world space

the problem I'm running into is adding camera rotation, when I rotate the camera I want player movement to work matching the rotation of the camera, but I do not want to change rotation away from looking at the current mouse point... how would I achieve this?

currently this playermovement script is attached to player object:

Code:
using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
    CharacterController _controller;
    Camera _maincam;
   
    [SerializeField]
    float _moveSpeed = 5.0f;
   
    [SerializeField]
    float _walkSpeed = 2.0f;
   
    [SerializeField]
    float _runSpeed = 10.0f;
   
    [SerializeField]
    float _gravity = 1.0f;
   
    float _yVelocity = 0.0f;
    bool _walking = false;

    public Quaternion _lookrotation;
   
   
    void Start ()
    {
        _controller = GetComponent<CharacterController>();
        _maincam = Camera.main;
    }
   
    void FixedUpdate ()
    {
        float h = Input.GetAxis ("Horizontal");
        float v = Input.GetAxis ("Vertical");

        MouseLook();
        Move (h,v);
        Debug.Log (_maincam.transform.rotation);
    }


    void Move(float h, float v)
    {
        Vector3 direction = new Vector3(h, 0, v);
        Vector3 velocity = direction * _moveSpeed;
       
        if (Input.GetKeyDown ("left ctrl"))
        {
            _walking = !_walking;
        }
       
        if (_walking == true)
        {
            velocity = direction * _walkSpeed;
        }
       
        if (Input.GetKey ("left shift"))
        {
            velocity = direction * _runSpeed;
        }
       
       
        _yVelocity -= _gravity;
       
        velocity.y = _yVelocity;
       
        _controller.Move (velocity * Time.deltaTime);

    }

    void MouseLook()
    {
        Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);

        RaycastHit mouseloc;

        if(Physics.Raycast(camRay,out mouseloc, 100))
        {
            Vector3 playertomouse = mouseloc.point - transform.position;

            playertomouse.y = 0f;

            _lookrotation = Quaternion.LookRotation(playertomouse);

            transform.localRotation = _lookrotation;
        }
    }
}

and this cammovement is attached to the camfocus object (also handles a lerped camera movement to follow character)

Code:

using UnityEngine;
using System.Collections;

public class CamMovement : MonoBehaviour
{
    public Transform _targetpos;
    public float _camsmoothing = 5f;

    float _camrotation;
    Vector3 _currentrot;

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

    void FixedUpdate ()
    {
        Vector3 targetcampos = _targetpos.position;
        transform.position = Vector3.Lerp (transform.position, targetcampos, _camsmoothing * Time.deltaTime);

        _camrotation = Input.GetAxis("Rotation");
        _currentrot = transform.localEulerAngles;
        _currentrot.y += _camrotation * _camsmoothing;
        transform.localEulerAngles = _currentrot;
    }
}


Please set me on the right path to achieve this, been stuck for a couple days.
Back to top
Permissions in this forum:
You cannot reply to topics in this forum