- Home /
GameObject look at velocity (not rigidbody)
Hi, I'm trying to make my own projectile script for an arrow but I'm having trouble looking at the velocity of it. This just keeps looking straight forward:
using UnityEngine;
using System.Collections;
public class ArrowScript : MonoBehaviour {
Transform myTransform;
Vector3 lastPosition;
Vector3 worldVelocity;
public bool lookAtVelocity;
public LayerMask layerMask;
public float drag = 0.01f;
public float gravity = -9.81f;
public Vector3 velocity;
void Awake () {
myTransform = GetComponent<Transform>();
lastPosition = myTransform.position;
}
void Update () {
worldVelocity = myTransform.position - lastPosition;
velocity.z -= drag * velocity.z * Time.deltaTime;
velocity.y += gravity * Time.deltaTime;
myTransform.position += myTransform.forward * velocity.z * Time.deltaTime;
myTransform.position += Vector3.up * velocity.y * Time.deltaTime;
if (lookAtVelocity) {
myTransform.LookAt(myTransform.position + worldVelocity);
}
lastPosition = myTransform.position;
}
}
I used to have the script to look at "myTransform.position + velocity" but velocity kinda works in world space so the arrow would always go straight world z axis. Any help regarding this issue?
Thanks in advance.
Edit: One thing tho, it is looking to the direction that the arrow is fired, but it doesn't start looking down as it starts dropping
Comment
velocity is world space yes but you can probably use the normalized version?
velocity.normalized