- Home /
draw line between mouse and gameobject on flat plane
Hi there, I'm trying to create a tennis game where the player moves the racket with their mouse.
I know how I want to achieve this, I don't know how to implement it. I first tried toying around with capnbishops mouse look from the wiki (http://wiki.unity3d.com/index.php?title=LookAtMouse) but this doesn't really give the result I was looking for.
The way I thought to do it is as follows. Have the character in the centre of the screen, with the camera following them as per a 3rd person controller. On the character transforms x and y axis, draw a line from the mouse cursor to the characters centre. Have the tennis racket position itself along this line, with the handle of the racket always closer to the character and the distance from the character determined by the mouse distance (in accordance with some form of proportion and max distance limit). This way the character can 'wave' with the racket in an arc, going from their left hand side, to above them, to their right hand side. I don't want the racket to move around in the z axis.
From what I could understand of the mouselook script, its draws this flat plane around the transform, and then takes the raycast position of the mouse as it intersects this plane, which sounds about right for me. But in game it just seems off. I think maybe I want the rackets transform to rotate on its z axis towards the hitpoint of the raycast? How do?
to save you clicking, here is the mouselook code: using UnityEngine; using System.Collections;
public class LookAtMouse : MonoBehaviour
{
// speed is the rate at which the object will rotate
public float speed;
void FixedUpdate ()
{
// Generate a plane that intersects the transform's position with an upwards normal.
Plane playerPlane = new Plane(Vector3.up, transform.position);
// Generate a ray from the cursor position
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Determine the point where the cursor ray intersects the plane.
// This will be the point that the object must look towards to be looking at the mouse.
// Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
// then find the point along that ray that meets that distance. This will be the point
// to look at.
float hitdist = 0.0f;
// If the ray is parallel to the plane, Raycast will return false.
if (playerPlane.Raycast (ray, out hitdist))
{
// Get the point along the ray that hits the calculated distance.
Vector3 targetPoint = ray.GetPoint(hitdist);
// Determine the target rotation. This is the rotation if the transform looks at the target point.
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
// Smoothly rotate towards the target point.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
}
By changing what I was searching for I came up with this:
using UnityEngine;
using System.Collections;
public class Sword$$anonymous$$over : $$anonymous$$onoBehaviour {
public Transform start;
public Transform end;
public Vector3 dir;
public Vector3 mid;
public float factor = 1.0f;
void Start() {
SetPos(start.position, end.position);
}
void Update () {
SetPos(start.position, end.position);
}
void SetPos(Vector3 start, Vector3 end) {
dir = end - start;
mid = (dir) / 2.0f + start;
transform.position = mid;
transform.rotation = Quaternion.FromToRotation(Vector3.up, dir);
}
}
By moving the transform that is set as 'end' you get the effect that I want. Now I just need to figure out how to change the end transform into mouse position and set a max distance.
I think i can change it to mouse position by s$$anonymous$$ling the raycast code from above and set the max by using if mid > 'maxlimit' mid = maxlimit.
I don't need the z position of the mouse, in fact I want the z position of the character to be used. I was thinking of changing start into a vector3 with the x and y being getaxis.vertical/horizontal and the z being the z axis of the start transform, but using
Vector3 end = new Vector3 (Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), start.transform.position.z); returns all sorts of errors.
amy help on implementing either of these would be great.
Your answer
Follow this Question
Related Questions
Plane.Raycast for mouselook won't work in positive y-axis 1 Answer
restrict mouselook X movement script does not work 4 Answers
If (increasing/decreasing)? check if something is increasing or decreasing 1 Answer
Mouse look Y axis sensitivity doesn't change 0 Answers
A projectile problem... (Mouse click using Ray/Plane) C# 0 Answers