- Home /
How to make main camera move towards player if it hits a game object.
How do I make a GTA style camera where if it hits the wall, it moves in, then when I move away, the camera moves back to its original position.
I don't know how to do it. and thanks in advanced
I'm also working on something like this, the best solution i found for now is to add a sphere collider and a rigidbody to the camera, so it don't go into the walls, but there should be a beter solution...
Im have that, but I want the player to be able to move all the way to the wall, not stop because the camera stops.
I see... a possibility is to make a script that try to move the position of the camera :
public class FollowGameobject : $$anonymous$$onoBehavior
{
public Transform toFollow;
Vector3 toFollowLastPos;
Vector3 deltaPos
{
get{ return toFollow.position - toFollowLastPos; }
}
Rigidbody rig;
void Start()
{
rig = GetComponent<Rigidbody>();
}
void Update()
{
rig.$$anonymous$$ovePosition(transform.position + deltaPos);
toFollowLastPos = toFollow.position;
}
}
this shouln't stop the player if camera is against the wall, but the camera will stay at her actual position if the player move away from the wall.
I give you this while writting the rest ofn the script...
Okay, there is the script, but i can't test it on the computer i actually have(....)
using UnityEngine;
//...
public class FollowGameobject : $$anonymous$$onoBehavior
{
public Transform toFollow;
float distanceToPlayer;
Vector3 toFollowLastPos;
Vector3 deltaPos
{
get { return toFollow.position - toFollowLastPos; }
}
float height
{
get { return toFollow.position.y - transform.position.y}
}
float distanceOnXZ
{
//pythagore
get { return $$anonymous$$athf.Sqr($$anonymous$$athf.Pow(distanceToPlayer, 2) - $$anonymous$$athf.Pow(height, 2)); }
}
Rigidbody rig;
bool isColliding = false;
void Start()
{
rig = GetComponent<Rigidbody>();
toFollowLastPos = toFollow.position;
distanceToPlayer = Vector3.Distance(toFollowLastPos, transform.position);
}
void Update()
{
if (!isColliding)
{
Vector3 centerAtHeight = new Vector3(toFollow.position.x, transform.position.y, toFollow.position.z);
Vector3 vectorCenterToPos = transform.position - centerAtHeight;
Vector3 vectorToAddFromCenterToGetPos = vectorCenterToPos.normalized * distanceOnXZ;
rig.$$anonymous$$ovePosition(centerAtHeight + vectorToAddFromCenterToGetPos);
}
else
rig.$$anonymous$$ovePosition(transform.position + deltaPos);
toFollowLastPos = toFollow.position;
}
void OnCollisionStay(Collision col)
{
//check of the good layer?
isColliding = true;
}
void OnCollisionExit(Collision col)
{
//check layer...
isColliding = false;
//no need for check if the object is multi colliding, if it's the case, 'OnCollisionStay' will put it backto false
}
}
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Detecting if background is clicked 1 Answer
Help!Crash! FatalError"Callback registration failed kMaxCallback" 1 Answer
Collider/Rigidbody issue.Camera shaking! 3 Answers
Distribute terrain in zones 3 Answers