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