Hi guys! I’m pretty new to all this stuff so here it goes… Im currently making a 3rd Person drone shooter game, where you fly a drone around and shoot loads of stuff… Anyway, I’m trying to get the y rotation of the drone to face the mouse position, so the player can use w,a,s,d to move in the 4 axis, q to lower the drones height and e to raise the drones height. Obviously, once the drone has rotated to face the mouse, this must become the new forward position.
Now, my question is, how do I get this rotation to work?? I found this coding as a solution elsewhere, but I can’t seem to get it to work, it appears the ‘speed’ variable is messing it up somehow? Heres my code in csharp: (By the way ignore the particle system stuff thats for the particle systems under the drones boosters to make it look funky!)
using UnityEngine;
using System.Collections;
public class DroneMovement : MonoBehaviour {
public float slowspeed = 2.0f;
public float fastspeed = 100.0f;
private GameObject Thruster1PS;
private GameObject Thruster2PS;
private GameObject Thruster3PS;
private GameObject Thruster4PS;
private bool isflying;
private Rigidbody PlayerRigidbody;
public float speed;
private int floorMask;
// Use this for initialization
void Awake ()
{
Thruster1PS = GameObject.Find ("particle_system_001");
Thruster2PS = GameObject.Find ("particle_system_002");
Thruster3PS = GameObject.Find ("particle_system_003");
Thruster4PS = GameObject.Find ("particle_system_004");
isflying = false;
PlayerRigidbody = GetComponent<Rigidbody> ();
floorMask = LayerMask.GetMask ("Floor");
}
// Update is called once per frame
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);
}
}
}
Any ideas why this won’t work?? Or what is a better way to do it? Also, what is the best way to tackle moving the drone? Thanks for your help in advance, Ben