• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by john 6 · Feb 23, 2011 at 10:32 PM · mousetopdownshooterwalking

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); } }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How do i make the character look the mouse 0 Answers

2D platformer cursor 1 Answer

Top down shooter animation, base off velocity and not input? 1 Answer

Top-Down 2D game:How to make the camera move on hitting the sides of the screen? 2 Answers

on trigger in top down shooter 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges