Running into an issue where my object is moving extremely fast when using a direction vs when theres a point to hit from raycast.
public static Vector3 GetShotDirectionCamera(Transform shootTransform, Camera camera, LayerMask mask)
{
float distance = 1000;
Ray ray = camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
if (Physics.Raycast(ray, out RaycastHit hit, distance, mask))
{
return hit.point;
}
else
{
return ray.GetPoint(distance);
}
}
I made sure to normalize the result so im not sure what to do.
newProjectile.Move(shotDirection - ShotOrigin.position.normalized, Profile.ProjectileSpeed);
public void Move(Vector3 moveDirection, float moveSpeed)
{
StartCoroutine(HandleMove(moveDirection, moveSpeed));
}
private IEnumerator HandleMove(Vector3 moveDirection, float moveSpeed)
{
float timeElapsed = 0;
while ((true))
{
transform.position += moveDirection * moveSpeed * Time.deltaTime;
timeElapsed += Time.deltaTime;
yield return null;
}
}
billygamesinc:
Running into an issue where my object is moving extremely fast when using a direction vs when theres a point to hit from raycast.
public static Vector3 GetShotDirectionCamera(Transform shootTransform, Camera camera, LayerMask mask)
{
float distance = 1000;
Ray ray = camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
if (Physics.Raycast(ray, out RaycastHit hit, distance, mask))
{
return hit.point;
}
else
{
return ray.GetPoint(distance);
}
}
I made sure to normalize the result so im not sure what to do.
newProjectile.Move(shotDirection - ShotOrigin.position.normalized, Profile.ProjectileSpeed);
public void Move(Vector3 moveDirection, float moveSpeed)
{
StartCoroutine(HandleMove(moveDirection, moveSpeed));
}
private IEnumerator HandleMove(Vector3 moveDirection, float moveSpeed)
{
float timeElapsed = 0;
while ((true))
{
transform.position += moveDirection * moveSpeed * Time.deltaTime;
timeElapsed += Time.deltaTime;
yield return null;
}
}
This
newProjectile.Move(shotDirection - ShotOrigin.position.normalized, Profile.ProjectileSpeed);
Is equivalent to this
var v0 = shotDirection;
var v1 = ShotOrigin.position.normalized;
newProjectile.Move(v0 - v1, Profile.ProjectileSpeed);
You forgot to use parentheses to get the normalized value from the delta.newProjectile.Move((shotDirection - ShotOrigin.position).normalized, Profile.ProjectileSpeed);
2 Likes
Spy-Master:
This
newProjectile.Move(shotDirection - ShotOrigin.position.normalized, Profile.ProjectileSpeed);
Is equivalent to this
var v0 = shotDirection;
var v1 = ShotOrigin.position.normalized;
newProjectile.Move(v0 - v1, Profile.ProjectileSpeed);
You forgot to use parentheses to get the normalized value from the delta.newProjectile.Move((shotDirection - ShotOrigin.position).normalized, Profile.ProjectileSpeed);
That fixed it, thank you!