- Home /
top down shooter help
hello, i am making a game that is a top down shooter and i have a quick question. How do i adjust my script so my character walks in the direction that the mouse is facing? I can get him to face the direction of the mouse, but not go in that direction. For example, he may be facing southeast, but if i press up, instead of walking in that direction, he goes north! can someone please help me out here? here is my script so far(in C#):
using UnityEngine; using System.Collections;
public class Walking: MonoBehaviour { //game objects (variables which point to game objects) private GameObject objPlayer; private GameObject objCamera; //input variables (variables used to process and handle input) private Vector3 inputRotation; private Vector3 inputMovement; //identity variables (variables specific to the game object) public float moveSpeed = 100f; private bool thisIsPlayer; // calculation variables (variables used for calculation) private Vector3 tempVector; private Vector3 tempVector2;
// Use this for initialization void Start () { objPlayer = (GameObject) GameObject.FindWithTag ("Player"); objCamera = (GameObject) GameObject.FindWithTag ("MainCamera"); if (gameObject.tag == "Player") { thisIsPlayer = true; }
}
// Update is called once per frame void Update () { FindInput(); ProcessMovement(); if (thisIsPlayer == true) { HandleCamera(); }
} void FindInput() { if (thisIsPlayer == true) { FindPlayerInput(); } else{ FindAIinput(); } } void FindPlayerInput() { //find vector to move// inputMovement = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
//find vector to the mouse tempVector2 = new Vector3(Screen.width * 0.5f,0,Screen.height *0.5f);
//find the postion of the middle of the screen
tempVector = Input.mousePosition ;
//find th position of the mouse on screen
tempVector.z = tempVector.y;
//input mouse position gives us 2Dcoordinates, I am moving the Y coordinate to the Z coorindate in temp Vector and setting the Ycoordinate to 0, so that the Vector will read the input along the X (left and right of screen) and Z(up and down screen) axis, and not the X and Y (in and out of screen) axis
tempVector.y = 0;
//Debug.Log(tempVector);
inputRotation = tempVector2 - tempVector;
//the direction we want to face/aim/shoot is from the middle of the screen where the mouse is pointing
}
void FindAIinput() { } void ProcessMovement()
{ rigidbody.AddForce (inputMovement.normalized moveSpeed Time.deltaTime); transform.rotation = Quaternion.LookRotation(inputRotation); transform.eulerAngles = new Vector3(0,transform.eulerAngles.y + 180,0); transform.position = new Vector3(transform.position.x,0,transform.position.z); } void HandleCamera() { objCamera.transform.position = new Vector3(transform.position.x,5,transform.position.z); objCamera.transform.eulerAngles = new Vector3(90,0,0); } }
Your answer
Follow this Question
Related Questions
How do i make the character look the mouse 0 Answers
Orthographic TopDown game Shooting Direction 1 Answer
Top Down Shooter Aiming 2 Answers
need help with making player look at mouse 3 Answers
2D TOP DOWN - W A S D as direction 2 Answers