Unity 2D jump in mouse direction
I like to write a scrip (C#) to move a game object in the mouse direction no constant force more a jump or teleport
im not good but i think it should work but it dont work and it make some strange thinks ;D can somebody help me ? This is my script Very simple script
using UnityEngine; using System.Collections;
public class jump : MonoBehaviour {
public float range ;
public Vector2 posi;
void Update (){
posi = Input.mousePosition * range;
if (Input.GetKeyDown (KeyCode.Q)){
transform.position =posi;
}
}
}
The mouse position is 1 point.
A direction requires 2 points.
So, which direction do you want?
You should use comment to answer me, not a real Answer.
I don't understand what you are requesting.
First of all, you want to a GameObject in the mouse direction, did you mean mouse position?
For that, you can use things like that:
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
It converts the position of the mouse on the screen (2 dimensions) in the world (3 dimensions).
It walk in 2d Do you know the flash from leuge of legends ? I like to make a game like Zelda i would make a jump and the aim of the jump is the mouse Direktion thed you jump with a max range in de Direktion of the mouse
Answer by Mikilo · Sep 01, 2015 at 11:26 AM
First of all, you have the position of your GameObject character.
When you press Q, you need to get the position of where the mouse is pointing on your terrain, map, ground, or anything like that.
With that new point, you now have a direction. Just move your GameObject towards the direction, handling when you are the range is too high or not.
To achieve that, you can do this:
RaycastHit hit;
if (Physics.Raycast(Camera.main, Camera.main.transform.forward, out hit, float.MaxValue) == true)
{
// Where you want to go.
Vector3 direction = (hit.point - transform.position);
// If higher than the range, clamp it.
if (direction.magnitude > range)
transform.position = transform.position + direction.normalized * range;
else
transform.position = transform.position + direction;
}
thenks for the great help ;) if you like i send you the game if it is ready ;D
A RPG like Zelda but with a bit mor focus on storry :)
Answer by FlyElephant · Sep 01, 2015 at 10:34 AM
The mouse input Position or is this wrong ?
always the current position of my mouse but only if i pres q the jump
always the current position of my mouse on the screen