Hi, I’m trying to make a game kinda like Star Wars Rogue Squadron mixed with a pinch of Descent. But I’ve some issues…
#1
I can’t make Quaternion rotation work
I’ve never tried this before, so I honestly don’t really know how to make this work… I’ve already looked at UnityScriptReference and it helped me at least fix two lines of the code, but I’m not sure what to do about Quaternion tempRot = target.transform.rotation + rotation; it says that Operator ‘+’ cannot be applied to operands of type ‘Quaternion’ and ‘Quaternion’
using UnityEngine;
using System.Collections;
public class SmoothCamFollow : MonoBehaviour {
public GameObject target;
public float smooth;
public Vector3 position;
public Quaternion rotation;
void FixedUpdate () {
Vector3 tempPos = target.transform.position + position;
transform.position = Vector3.Lerp(transform.position, tempPos, Time.deltaTime * smooth);
transform.LookAt(target.transform);
}
}
#2
How to make a space ship fly after a crosshair? I’m trying to make my game like this:
but I can’t make my space ship fly in the direction that I point my mouse towards (following the crosshair). I’ve tried to use different scripts but I can’t make anything work… I’ve been stuck at this for a while now ![]()
using UnityEngine;
using System.Collections;
public class LookAtMouse : MonoBehaviour {
public GameObject player;
public float speed;
void FixedUpdate() {
Plane player = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
// If the ray is parallel to the plane, Raycast will return false.
if (player.Raycast(ray, out hitdist)) {
// Get the point along the ray that hits the calculated distance.
Vector3 targetPoint = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
}
-------------------- and I’ve also tried this, which doesn’t work either -------------
using UnityEngine;
using System.Collections;
public class CamLook : MonoBehaviour {
public float sensitivity = 1f;
public float minimumY = -60f;
public float maximumY = 60f;
private float rotationY = 0f;
private float rotationX = 0f;
void Update() {
rotationX = transform.localEulerAngles.y + Input.GetAxis(“Mouse X”) * sensitivity * Time.deltaTime;
rotationY += Input.GetAxis(“Mouse Y”) * sensitivity * Time.deltaTime;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
}
