Rigidbody velocity angle.

Hello, I am stuck on velocity angle. I can make rigidbody make move forward, but what i need is to make it to move in the direction where i clicked with mouse.

using UnityEngine;
using System.Collections;

``````public class RMBSpell : MonoBehaviour {
public CharacterController player;
public float speed = 10f;
public Rigidbody effect1Moving;
public float timer = 60;
public float damage;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(1))
{
Quaternion rot = Quaternion.FromToRotation(player.transform.position, Input.mousePosition);
rot.x = 0f;
rot.z = 0f;
Rigidbody clone;
Vector3 targetDir = Input.mousePosition - player.transform.position;
Vector3 forward = transform.forward;
float angle = Vector3.Angle(targetDir, forward);
clone = Instantiate(effect1Moving, new Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z), rot) as Rigidbody;
clone.velocity = transform.TransformDirection(Vector3.forward * speed);
}

}
}
``````

I made something similar to what you aim to achieve using raycasting. So, basically it casts a ray from “ScreenPointToRay” (the mouse position), then while the distance between the player object and the raycasthit.point (position of raycast’s collision with ground) is greater than 0.01 it initiates a Lerp(linear interpolation). This is activated through the calling of the coroutine in the Update(). It just seemed less listy than your current arrangement. Let me know if this isn’t what you want.

``````public class RMBSpell : MonoBehaviour {
public CharacterController player;
public RayCastHit hit;
private Vector3 clickPos;

// Use this for initialization
void Start () {

}

IEnumerator MovePlayer ()
{
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
{
clickPos = hit.point;

while (Vector3.Distance(clickPos, player.transform.position)>0.01f)
{

player.transform.position = Vector3.Lerp(player.transform.position,clickPos, Time.deltaTime * speed);
yield return new WaitForEndOfFrame();
}

}

}

}

// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(1))
{
StartCoroutine(MovePlayer());
}

}
}
``````