- Home /
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);
}
}
}
How this is solved will depend highly on the mechanics of the game.
Orthographic vs Perspective camera?
Angle of the camera with respect to the world axes?
How you want to map the 2D mouse position into 3D space?
The big problem is on line 21. Input.mousePosition is in screen coordinates, but your player.transform.position is in world coordinates. Start by looking at Camera.ScreenToWorldPoint(). Depending on your answers to the questions, this may not be the right solution.
camera is isometric like in h'n's games script:
// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we
var heightDamping = 2.0;
//var rotationDamping = 3.0;
// Place the script in the Camera-Control group in the component menu
@script AddComponent$$anonymous$$enu("Camera-Control/Smooth Follow")
function LateUpdate () {
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
//wantedRotationAngle = target.eulerAngles.y;
wantedHeight = target.position.y + height;
//currentRotationAngle = transform.eulerAngles.y;
currentHeight = transform.position.y;
// Damp the rotation around the y-axis
//currentRotationAngle = $$anonymous$$athf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = $$anonymous$$athf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
//currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -=Vector3.forward * distance;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
transform.LookAt (target);
}
Camera.ScreenToWorldPoint() dont work
I'm out of time, so I cannot give you a detailed answer. The best/usual solution for this problem is to use Unity's mathematical Plane class to construct a mathematical plane through the pivot point of your character. Then you can use the Plane's Raycast() function to find a position. Quaternion.LookRotation() will give you the rotation. Code to do this Raycast() against a Plane to find the mouse position has been posted before, so Google might be your friend. You may want to use Quaternion.Slerp() or Quaternion.RotateTowards() to soften the rotation.
Answer by putlucky · Apr 11, 2014 at 12:06 AM
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());
}
}
}
But it's script to move character controller? Created another C# trigger and its what i get error: Assets/Created/Scripts/R$$anonymous$$B2.cs(35,6): error CS0116: A namespace can only contain types and namespace declarations