I have a rocket ship(imported from sketch. it will be my avatar), it moves up at the moment. The camera does not follow, I want to write a C# script that will pull position data from the rocket and with a specific range change, it will jump and follow. I know generally what I want to do, but I am at a loss as to how i can call tranform.position.x/y/z.
I just thought of this, would i create a function under “update” function in the script controlling the rocket that will return transform.position or alternative x, y, z in 3 different functions. which i could try to call from the script that is to be applied to the camera?
public Vector3 rocketPosition(){ return this.transform.position; }
Edit: Attempting the above is not working, i will continue spitballing this approach
(I am sure there is plugin or asset that will easily allow me to have the camera follow, but I want to rely on my work so I learn more as I am amateur enough that I need to put some work in to get better)
I admire your will to create things yourself. I do the same thing with the video game I’m developing, (arxcatalyst.weebly.com). Lets see if I can cover all the bases of your question. To access another objects information all you have to do is use a gameobject variable. Excuse the javascript, I’m very rusty in C#. for instance, on your camera script you can do this:
var MyRocket : GameObject;
function Update()
{
if (MyRocket.transform.y >= 10)
Debug.Log("follow that rocket!");
}
now then, to call a user made function you can send messages between objects. Let’s take a look at a modified version of the previous example.
var MyRocket : GameObject;
var rocketAltitude : float;
function Update()
{
rocketAltitude = MyRocket.SendMessage("ReportAltitude")
if(rocketAltitude >= 10)
Debug.Log("follow that rocket!");
}
So the format for SendMessage is like so:.SendMessage(“”)
I hope this leads you to your answers, and I applaud you on your desire to learn instead of copy.
This is my code for my camera, it does not work. im debugging.
using UnityEngine;
using System.Collections;
public class camerafollow : MonoBehaviour {
float x=0, y=0,z=0, thisx=0, thisy=0, thisz=0;
public moveRock rocketpos;
public Camera thisC;
// Use this for initialization
void Start () {
rocketpos= GetComponent<moveRock>();
// object.Getcomponent<script>();
}
// Update is called once per frame
void Update () {
//x and y camera control.
x=rocketpos.transform.position.x;
y=rocketpos.transform.position.y;
//z=rocketpos.transform.position.z;
thisx=thisC.transform.position.x;
thisy=thisC.transform.position.y;
//thisz=this.transform.position.z;
if( thisx<(x-20))
{
thisx+=30;
thisC.transform.position += Vector3.right*thisx;
}
if( thisx>(x+20))
{
thisx-=30;
thisC.transform.position += Vector3.right*thisx;
}
if( thisy<(y-20))
{
thisy+=30;
thisC.transform.position += Vector3.up*thisy;
}
if( thisy>(y+20))
{
thisy-=30;
thisC.transform.position += Vector3.up*thisy;
}
}
}
I can’t completely visualise what you’re looking for with the range change
Here is a most basic camera follow script. It moves the camera to the same position as the rocket with a distance (10).
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour
{
public Transform theRocket; // Drag the rocket into here. Gets the rocket's Transform component (Pos/Rot/Scale) without needing another function
void Update()
{
// Create a new Vector3 to hold rocket's Transform > Position XYZ
Vector3 camPosition = theRocket.position;
// Access the Z value. Take away a number so the camera is further away from the rocket.
camPosition.z = theRocket.position.z - 10f;
// camPosition is useless unless you apply it back to the camera
transform.position = camPosition;
}
}
This one will follow more lazily.
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour
{
public Transform theRocket; // Drag the rocket into here. Gets the rocket's Transform component (Pos/Rot/Scale) without needing another function
private Vector3 camPosition;
void Update()
{
// Make camPosition hold rocket's Transform > Position XYZ
camPosition = Vector3.Lerp(camPosition, theRocket.position, 1f * Time.deltaTime);
// Access the Z value. Take away a number so the camera is further away from the rocket.
camPosition.z = Mathf.Lerp(camPosition.z, theRocket.position.z - 10f, 1f * Time.deltaTime);
// camPosition is useless unless you apply it back to the camera
transform.position = camPosition;
}
}