Question by
finalsin · Feb 04, 2016 at 11:58 PM ·
rotationcharactercontrollermouselook
Transform.LookAt stops working after CharacterController.Move is used.
If I don't move my character (A Cube with a CharacterController attached to it) , my rotation script works perfectly, being it looks at the Mouse Cursor. Once i make any movement using CharacterController.Move, it instantly freaks out and stops following the mouse , but still makes small movements when my mouse is moved. Any ideas?
The view is a top down shooter, the "gun" will follow the mouse and point at enemies
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
CharacterController Char;
public float MoveSpeed = 15;
// Use this for initialization
void Start()
{
Char = this.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
float x = 0;
float z = 0;
if (transform.position.y != 1)
transform.position = new Vector3(transform.position.x, 1, transform.position.z);
if (Input.GetKey(KeyCode.W))
{
z = 1;
}
if (Input.GetKey(KeyCode.S))
{
z = -1;
}
if (Input.GetKey(KeyCode.D))
{
x = 1;
}
if (Input.GetKey(KeyCode.A))
{
x = -1;
}
Char.Move((new Vector3(x, 0, z) * Time.deltaTime) * MoveSpeed);
Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1));
transform.LookAt(mousePos, Vector3.up);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to disable mouse look in Unity 5? 2 Answers
Rotating game object based on mouse drag direction position 0 Answers
Aim at my cursor 1 Answer
FPS Character Controller HELP with head bob and camera! 2 Answers