Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 Option3 · Feb 07, 2017 at 01:35 PM · 2dscripting beginnermovecoordinates

Instantly move my player to a position I click on (2d)

I have a script on my player object that sets a bool named "charge" to true when I click on it. This is a precondition to moving my player somewhere else in the play area which is determined by where I click next.

2d topdown view, player and background

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerCharge : MonoBehaviour {
 
 
     private bool charged;
 
 
     // Use this for initialization
     void Start () {
         charged = false;
     }
         
     void Update ()
     {
         //Makes the player spin after it gets charged up
 //        if (charged !=false) {
 //        transform.Rotate (0,0,1440*Time.deltaTime); //rotates 50 degrees per second around z axis
 //        }
     }
 
 
     // Click the player to set charge it up
     void OnMouseDown () {
         if (charged != true) {
             charged = true;
 //            chargedSpin ();
             Debug.Log ("Charged Up!");
         } 
         else Debug.Log ("already"); 
     }
         
 
 }
 

This first part works, but now I want to: Create a new script on the background sprite that captures my click and stores the transform info for where I clicked, then, if my player object has the charge = true state, instantly place my player at the position I clicked.

I'm having trouble with the approach - would I be right to put that script on the background sprite? if so, how can I a) get the variable from my player script and b) move my player object using this script.

I've tried looking into how to get scripts to communicate with each other but I'm having no joy.

screen-shot-2017-02-06-at-103249-am.png (414.4 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by JaredHD · Feb 07, 2017 at 02:38 PM

I would use a Raycast from the camera to the mouse position. Then if charged is true move the player to the Raycasted area.

So something like this:

 using System;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     bool charged = false;
 
     private void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             RotatePlayer();
             MovePlayer();
         }
     }
 
     private void RotatePlayer()
     {
         //Your rotate script
     }
 
     private void MovePlayer()
     {
         //Creates a Ray from Camera to mouse
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         //Used to store the Raycast hit
         RaycastHit hit;
 
         //If we hit something place that in the "hit" variable and if charged is true move to point
         if (Physics.Raycast(ray, out hit) && charged == true)
         {
             transform.position = hit.point;
         }
     }
 }

That goes on the player.

Bonus: How to communicate with different scripts?

Lets say you have a script on the player and you have a script on an enemy. You press on the enemy and want to know how much health he has and display it to the Console. You would use the Debug.Log(GameObject.Find("Enemy").GetComponent<EnemyAi>().health);

GameObject.Find("Transform Name") is the easy way to do and not always recommended. Rather use tags. This finds the enemy that has the script attached to it.

.GetComponent().Variable; That retrieves the specific Script by Class name and the variable you want to edit/read. You can use Methods instead of variables as well

Comment
Add comment · Show 2 · Share
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
avatar image Option3 · Feb 12, 2017 at 08:09 PM 0
Share

Thanks for responding. I couldn't get that code to work, but I'll look into the approach and see if i can figure it out myself.

avatar image JaredHD Option3 · Feb 13, 2017 at 01:53 PM 0
Share

The Raycast would need to hit something like a terrain or cube. Here check out this Link and see if it might help.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D Character grabing the wall 1 Answer

2D Movement Problems 2 Answers

Find cardinal direction of collision based on 2D Cartesian coordinates 1 Answer

How to change objects position a set number of units from it's current position? 0 Answers

Unity2D move + turn with a fixed angle 1 Answer


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