- Home /
How to make a camera follow an object
Hey. I have 2 questions. 1 is how to make a camera follow an object controlled by wasd and arrow keys. The next thing is i am using a code to make a projectile shoot out of the object. it works fine but when i shoot it while facing east, it seems to get blocked and not shoot out. it'll just appear then fall down. here's my code
var speed=3.0; var rotateSpeed=3.0; var bulletPreFab:Transform;
function Update () { var controller:CharacterController=GetComponent(CharacterController);
//Rotate around y=axis
transform.Rotate(0, Input.GetAxis ("Horizontal")*rotateSpeed,0);
//Move forward/backward
var forward=transform.TransformDirection(Vector3.forward);
var curSpeed=speed*Input.GetAxis("Vertical");
controller.SimpleMove(forward*curSpeed);
if(Input.GetButtonDown("Fire1")) {
var bullet=GameObject.Instantiate(bulletPreFab, GameObject.Find("spawnpoint").transform.position,Quaternion.identity);
bullet.rigidbody.AddForce(forward.x * 4000,forward.z*4000,forward.y*4000);
}
}
@script RequireComponent(CharacterController)
For making the camera follow an object, add the Component/Camera-Control/SmoothFollow script (comes standard with Unity).
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Object parented to MainCamera and Y Axis Question: 0 Answers
camera goes through walls n objects etc 1 Answer
How to have camera follow x&y only of another object? 2 Answers
Have Arms follow camera partially 0 Answers