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 hary99 · Mar 29, 2017 at 03:24 PM · c#unity 5gameobjecttransformboxcollider2d

Moving player to generated position

I am fairly new to Unity, but I have gotten a fairly decent grip on the whole coding aspect. I have been cruising along the development of my game but I just simply can't get my player script to work properly. I've looked all over and I might just be ignorant, but I cannot find anything that fits what I am trying to do.

Movement I am trying to do: I have a generating set of game objects, and I want the player to appear that he is "Jumping" to the position (but this isn't a physics related came, so I am not using any rigid bodies). Basically what I want to do is get Player from Object1 (initial object) to Object1 (generated, but in a new position) by clicking. I do not want the jump to to happen if the player has not clicked the general area of the box collider.

I hope I provided a fairly detailed description.

Here is the code I currently have for my playerscript (I borrowed it off of a tutorial and changed a good bit of it, so you will see some "hints") Branch1 = Game Object

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour
 {
 
     public float speed = 1.5f;
     public Vector2 branch1;
     private Vector2 target;
 
     void Start()
     {
 
         target = transform.position; //gameobjects position before moving
         branch1 = GameObject.Find("branch1").transform.position;
 
     }
     void Update()
     {
 
         if (Input.GetMouseButtonDown(0)) //if user clicked somewhere on the screen
 
         {
 
 
             target = Camera.main.ScreenToWorldPoint(Input.mousePosition); //target (click) position
 
         }
             
         transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
 
 
     }
 
 }
 
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityCoach · Mar 29, 2017 at 04:01 PM

You want to player to move to the mouse click point, only if you clicked within a collider? If so, you can a) use Physics2D.RayCast, b) add a component to the Collider with a OnMouseDown method that'll have to send the click position to the player script.

Comment
Add comment · Show 4 · 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 hary99 · Mar 29, 2017 at 07:09 PM 0
Share

I'm gonna try method A in a bit here, but how would I do method B? Tad confused on that one, but I will try A. Cheers for the quick response btw!

avatar image UnityCoach hary99 · Mar 30, 2017 at 12:01 PM 0
Share

$$anonymous$$ethod B requires two components, one on the Collider that tells the one on the Player when it got clicked, using On$$anonymous$$ouseDown(), so that the latter can update its target position.

avatar image hary99 · Mar 29, 2017 at 08:37 PM 0
Share

I actually went a different route, so if anyone is willing to answer to this. But I switched back to my original build which uses physics. I have the jumping mechanism working just fine, but I am curious as to whether or not it is possible to change the direction of the jump? (Right now I am jumping just straight up) how would I make my player jump towards the clicked position on screen? (If any of this makes sense, please respond <3)

avatar image UnityCoach hary99 · Mar 30, 2017 at 12:01 PM 0
Share

by "toward" you mean orient itself toward the next location?

avatar image
0

Answer by dragonking300 · Mar 30, 2017 at 01:37 PM

Ok, even though i'm fairly new my self I will explain how to do that(probably)

Have your game generate the object and make sure the prefab for the object has a tag like "newPosition" or something along those lines

do something along the lines of this

 private GameObject newPosition
 public GameObject Player
 
 //^ put these variables before the functions
 
 public void InsertThisInsideYourGenerationScript()
 {
 //blah blah blah generation script blah blah blah
 //Then once your object gets generates
  newPosition = GameObject.FindGameObjectWithTag("Newposition");
 }
 
 private void Update()
 {
 if(newPosition = !null)
 {
 if(Vector3.Distance(Player.transform.position, newPosition.transform.position) <= 0.1)
 {
 //Now if you what to have your object teleport to their location replace Slerp with transform.position = newPosition
 
 transform.position = Vector3.Slerp(transform.position, newPosition, SpeedOfMovement * Time.deltatime);
 }
 }
 }



Comment
Add comment · 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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform a gameobject y rotation to another gameobject y rotation 1 Answer

Change gameobjects position if it's already used by a different gameobject? 1 Answer

Distribute terrain in zones 3 Answers

How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers

Calling an Audio Source on one game object from a script on another game object..? 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