- Home /
Look Behind Me While Still Moving Forward
I am trying to automatically run forward while being able to simultaneously look behind me (using input) however the problem I encounter is that when I press the button to look behind me my character starts running in that direction. I want the user to look behind them while still running forward. Please help.
This is the code to run forward
using UnityEngine; using System.Collections;
public class Run_Forward : MonoBehaviour {
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
     if (Input.GetKeyDown(KeyCode.A))
         transform.position += Vector3.right*4;
     
     if (Input.GetKeyDown(KeyCode.D))
         transform.position += Vector3.left*4;
     
     transform.Translate(0, 0, .2f);
     
 }
}
And this is the code to turn around
using UnityEngine; using System.Collections;
public class Rotate : MonoBehaviour { public Transform target;
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode.T))
         transform.LookAt(target);
 }
}
Answer by Piflik · Jun 06, 2012 at 10:32 PM
I would use a hierarchy of two objects to separate movement and look direction. The parent is used for the movement, and you rotate the child if you want to look back.
I actually attached the script to the camera that comes with the first person controller ins$$anonymous$$d of putting it on the first person controller itself. So far it seems to work fine. :)
Your answer
 
 
             Follow this Question
Related Questions
Lock rotation axis? 4 Answers
Move camera around object IOS 2 Answers
Move around object 1 Answer
LookAt() and RotateAround() 1 Answer
Transform.Rotate for Quaternions? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                