Hi, Unity Community! I’m working on a ball toss FPS game, where when the ball instantiates, it moves in an arc forwards. Now I’m trying to do the same thing when I rotate the camera to look in a different direction. How can I accomplish this? Thanks for any help! ![]()
Trajectory.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trajectory : MonoBehaviour {
public float speed = 20f;
public float distance = 10f;
public float angle = 45f;
public float angleSpeed = 20f;
float curAngle;
void Start(){
curAngle = angle;
}
void Update(){
angle = Mathf.Clamp (angle, -curAngle, curAngle);
angle += Time.deltaTime * -speed;
transform.position += transform.forward * Time.deltaTime * distance;
transform.rotation = Quaternion.AngleAxis (-angle, transform.right * Time.deltaTime * angleSpeed);
}
}
Instantiate Snippet.cs
GameObject obj = Resources.Load ("Prefabs/Primative Level Design/Tossable") as GameObject;
if (Input.GetButtonDown ("Cannon") && canShoot) {
GameObject clone = Instantiate (obj, Camera.main.transform.position + Camera.main.transform.forward * 5, Camera.main.transform.rotation);
}