- Home /
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);
}
}
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.
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!
$$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.
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)
by "toward" you mean orient itself toward the next location?
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);
}
}
}
Your answer
Follow this Question
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