How to create GTA or AC camera and controller scripting?

hi

I want to create GTA or Assassin Creed control and camera…

How do I write in code? This code is not exactly what I want:

var speed : float = 50.0;
var rotateSpeed : float = 3.0;

function Update () {
    var controller : CharacterController = GetComponent(CharacterController);

    // Rotate around y - axis
    
    
    transform.Rotate(0, Input.GetAxis ("A") * rotateSpeed, 0);
    controller.slopeLimit = 90;
    
     // Move forward / backward
    var forward : Vector3 = transform.TransformDirection(Vector3.forward);
    var curSpeed : float = speed * Input.GetAxis ("W");
    controller.SimpleMove(forward * curSpeed);
    
}

@script RequireComponent(CharacterController)

I want the character to act according to the angle of the camera.

When I press the camera according to the direction I want to move left and right keys.

please help…

(I m English bad!! sorry)

needed a system like this link:

http://dl.dropbox.com/u/2011649/Games/Ninja/Ninja.html

please heeeeelp!!! :frowning:

Make it Slerp or Lerp from the rotation it has, to the rotation in where your heading.

What I did is that.

I created a empty game Object that rotates.foward a vector 3 the direction where I am moving.

That Made the the camera slerp from its rotation to the empty game object rotation.

Im making one too, but its buggy as hell though.

Good Luck!

thanks my friend

Can you show me an example? I was still a novice :slight_smile:

How is your camera down? Does it have a MouseLook script?

I need to know how the camera is made to know if I could help you or not with the code.

But here is an example of what I did with Slerp.

transform.rotation = Quaternion.Slerp(transform.rotation,cameraController.transform.rotation, 10 * Time.smoothDeltaTime);

The main gameobject will rotate with 10 units per seconds smoothly to meet the cameraController rotation.

Ok saw the ninja example its more complicated than you think though.

First I recommend you to use Input.GetAxis(“Horizontal”) or vertical to do the movments.

The Rotations should be made by the direction your moving to not the controller.

You need to use Slerp like the example above to make the caracter smoothly rotate at the same direction the camera is facing.

You need lots of conditions so it doesnt bug on.

Use the Script Refrence guide to look the codes, first look on the runtime codes and study each one and give yourself an idea of how the work and what are they for. Some come with examples.

There is a controller movement same to the example you showed in the Script Refrence in the Input.GetAxis example.

So you need 1 gameObject that controllers all, a code that Aligns the main thing and the model to the direction your facing, a script that controls the camera.

Practice Practice and Practice, that the only way you will get with it, you could copy all the code from the internet that you like but you dont understand it your likely to fail.

Study Quaternion it will do you charms. And Inputs, Vector3, the the Mathf class.

Good luck.

Thank you for code , my friend :smile: This has really worked for me!.. code this:

script for the Camera= MouseOrbit

var speed : float = 50.0;
var cameraController : Transform;

function Update () {
    var controller : CharacterController = GetComponent(CharacterController);
    
    var forward : Vector3 = transform.TransformDirection(Vector3.forward);
    var curSpeed : float = speed * Input.GetAxis ("W");
    
    var leftright : Vector3 = transform.TransformDirection(Vector3.right);
    var curSpeed2 : float = speed * Input.GetAxis ("A");
    
    controller.SimpleMove(forward * curSpeed);
    controller.SimpleMove(leftright * curSpeed2);
    
    
if (Input.GetAxis("W"))
{
    transform.rotation = Quaternion.Slerp(transform.rotation,cameraController.transform.rotation, 5 * Time.smoothDeltaTime);
    transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
    animation.CrossFade("walk");
    }
    
else
    animation.CrossFade("idle");
    
if (Input.GetAxis("A"))
{
     transform.rotation = Quaternion.Slerp(transform.rotation,cameraController.transform.rotation, 5 * Time.smoothDeltaTime);
    transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
    
    }
}

@script RequireComponent(CharacterController)

but I have a problem…

How do I fix it? :face_with_spiral_eyes:

Ok here goes my thoughts.

First you need the player to move the same direction as where you are moving.

So you would use your CharacterController.velocity to give you back a Vector3.

var direction : Vector3 = controller.velocity;

make and empty gameobject right in the middle of your gameobject same position as your caracter (x,y,z) and rotation too.

make it child of your caracter.

in your code add

var  lookDirection : Transform; /// add the empty gameobject here.
var rotationSpeed : float = 5.0f; /// used to know how fast the caracter will rotate.

function Update{
lookDirection.trasform.forward = direction; /// looking at the same direction as direction.
transform.rotation = Quaternion.Slerp(transform.rotation,lookDirection.transform.rotation, rotationSpeed * Time.deltaTIme); // rotate caracter to the same rotation as the lookdirection.
}

Add the empty child gameObject to lookDirection.

This will make the empty game object that you just made look at the direction the controller is moving because it give back your direction your head to in vectores and the gameobject rotates to that direction.

We use the same lookDirection as it is looking forward the direction we are heading, we can take its rotation and make our Caracter rotate to the same direction. and it will rotate smoothly :slight_smile:

Now for the camera I did it difriently.

Instead of mouseOrbit I used mouselook Script.

Created an empty gameObject and child it to the caracter, with the mouseLook script edit it abit, and made the emptygameObject rotate, than child the camera to the empty gameObject, it will rotate with it doing pretty much the same.

Than with the emptyGameObject that I named cameraController, took its rotation to use in the transform.rotation then used Quaternion.Slerp like before and made the caracter Slerp to the same rotation as cameraController and Slerp the cameraController to so it doesnt look to rough.

excuse me my friend… I could not :frowning:

required to return the camera around my character… mouseOrbit this therefore necessary to I use…

No problem with the way the character left and right… but it does not turn 90 degrees to face direction (left and right)

Check out the “RPG Controller Camera” link in my signature.

thanks…

but I can not buy… :wink:

problem solved them with this code: :wink: (ThirdPersonController) I took out the unnecessary codes

// Require a character controller to be attached to the same game object
@script RequireComponent(CharacterController)

private var _characterState : CharacterState;

// The speed when walking
var walkSpeed = 2.0;
// after trotAfterSeconds of walking we trot with trotSpeed
var trotSpeed = 4.0;
// when pressing "Fire3" button (cmd) we start running
var runSpeed = 6.0;

var inAirControlAcceleration = 3.0;

// How high do we jump when pressing jump and letting go immediately
var jumpHeight = 0.5;

// The gravity for the character
var gravity = 20.0;
// The gravity in controlled descent mode
var speedSmoothing = 10.0;
var rotateSpeed = 500.0;
var trotAfterSeconds = 3.0;

var canJump = true;

private var jumpRepeatTime = 0.05;
private var jumpTimeout = 0.15;
private var groundedTimeout = 0.25;

// The camera doesnt start following the target immediately but waits for a split second to avoid too much waving around.
private var lockCameraTimer = 0.0;

// The current move direction in x-z
private var moveDirection = Vector3.zero;
// The current vertical speed
private var verticalSpeed = 0.0;
// The current x-z move speed
private var moveSpeed = 0.0;

// The last collision flags returned from controller.Move
private var collisionFlags : CollisionFlags; 

// Are we jumping? (Initiated with jump button and not grounded yet)
private var jumping = false;
private var jumpingReachedApex = false;

// Are we moving backwards (This locks the camera to not do a 180 degree spin)
private var movingBack = false;
// Is the user pressing any keys?
private var isMoving = false;
// When did the user start walking (Used for going into trot after a while)
private var walkTimeStart = 0.0;
// Last time the jump button was clicked down
private var lastJumpButtonTime = -10.0;
// Last time we performed a jump
private var lastJumpTime = -1.0;

// the height we jumped from (Used to determine for how long to apply extra jump power after jumping.)
private var lastJumpStartHeight = 0.0;
private var inAirVelocity = Vector3.zero;
private var lastGroundedTime = 0.0;
private var isControllable = true;


function UpdateSmoothedMovementDirection ()
{
	var cameraTransform = Camera.main.transform;
	var grounded = IsGrounded();
	
	// Forward vector relative to the camera along the x-z plane	
	var forward = cameraTransform.TransformDirection(Vector3.forward);
	forward.y = 0;
	forward = forward.normalized;

	// Right vector relative to the camera
	// Always orthogonal to the forward vector
	var right = Vector3(forward.z , 0, -forward.x);

	var v = Input.GetAxisRaw("W");
	var h = Input.GetAxisRaw("A");

	// Are we moving backwards or looking backwards
	if (v < -0.2){
	    animation.CrossFade("walk");
		movingBack = true;
		}
		else{
		movingBack = false;
		animation.CrossFade("idle");
                }
	
	var wasMoving = isMoving;
	isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
		
	// Target direction relative to the camera
	var targetDirection = h * right + v * forward;
	
	// Grounded controls
	if (grounded)
	{
		// Lock camera for short period when transitioning moving  standing still
		lockCameraTimer += Time.deltaTime;
		if (isMoving != wasMoving)
			lockCameraTimer = 0.0;

		// We store speed and direction seperately,
		// so that when the character stands still we still have a valid forward direction
		// moveDirection is always normalized, and we only update it if there is user input.
		if (targetDirection != Vector3.zero)
		{
			// If we are really slow, just snap to the target direction
			if (moveSpeed < walkSpeed * 0.9  grounded)
			{
				moveDirection = targetDirection.normalized;
			}
			// Otherwise smoothly turn towards it
			else
			{
				moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
				
				moveDirection = moveDirection.normalized;
			}
		}
		
		// Smooth the speed based on the current target direction
		var curSmooth = speedSmoothing * Time.deltaTime;
		
		// Choose target speed
		//* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
		var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
	
		_characterState = CharacterState.Idle;
		
		// Pick speed modifier
		if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift))
		{
			targetSpeed *= runSpeed;
			_characterState = CharacterState.Running;
		}
		else if (Time.time - trotAfterSeconds > walkTimeStart)
		{
			targetSpeed *= trotSpeed;
			_characterState = CharacterState.Trotting;
		}
		else
		{
			targetSpeed *= walkSpeed;
			_characterState = CharacterState.Walking;
		}
		
		moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
		
		// Reset walk time start when we slow down
		if (moveSpeed < walkSpeed * 0.3)
			walkTimeStart = Time.time;
	}
	// In air controls
	else
	{
		// Lock camera while in air
		if (jumping)
			lockCameraTimer = 0.0;

		if (isMoving)
			inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
	}
}


function ApplyJumping ()
{
	// Prevent jumping too fast after each other
	if (lastJumpTime + jumpRepeatTime > Time.time)
		return;

	if (IsGrounded()) {
		// Jump
		// - Only when pressing the button down
		// - With a timeout so you can press the button slightly before landing		
		if (canJump  Time.time < lastJumpButtonTime + jumpTimeout) {
			verticalSpeed = CalculateJumpVerticalSpeed (jumpHeight);
			SendMessage("DidJump", SendMessageOptions.DontRequireReceiver);
		}
	}
}


function ApplyGravity ()
{
	if (isControllable)	// don't move player at all if not controllable.
	{
		// Apply gravity
		var jumpButton = Input.GetButton("Jump");
		
		
		// When we reach the apex of the jump we send out a message
		if (jumping  !jumpingReachedApex  verticalSpeed <= 0.0)
		{
			jumpingReachedApex = true;
			SendMessage("DidJumpReachApex", SendMessageOptions.DontRequireReceiver);
		}
	
		if (IsGrounded ())
			verticalSpeed = 0.0;
		else
			verticalSpeed -= gravity * Time.deltaTime;
	}
}

function CalculateJumpVerticalSpeed (targetJumpHeight : float)
{
	// From the jump height and gravity we deduce the upwards speed 
	// for the character to reach at the apex.
	return Mathf.Sqrt(2 * targetJumpHeight * gravity);
}

function DidJump ()
{
	jumping = true;
	jumpingReachedApex = false;
	lastJumpTime = Time.time;
	lastJumpStartHeight = transform.position.y;
	lastJumpButtonTime = -10;
	
	_characterState = CharacterState.Jumping;
}

function Update() {
	
	if (!isControllable)
	{
		// kill all inputs if not controllable.
		Input.ResetInputAxes();
	}

	if (Input.GetButtonDown ("Jump"))
	{
		lastJumpButtonTime = Time.time;
	}

	UpdateSmoothedMovementDirection();
	
	// Apply gravity
	// - extra power jump modifies gravity
	// - controlledDescent mode modifies gravity
	ApplyGravity ();

	// Apply jumping logic
	ApplyJumping ();
	
	// Calculate actual motion
	var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0) + inAirVelocity;
	movement *= Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	collisionFlags = controller.Move(movement);
	
	// ANIMATION sector
	
	// Set rotation to the move direction
	if (IsGrounded())
	{
		transform.rotation = Quaternion.LookRotation(moveDirection);
			
	}	
	else
	{
		var xzMove = movement;
		xzMove.y = 0;
		if (xzMove.sqrMagnitude > 0.001)
		{
			transform.rotation = Quaternion.LookRotation(xzMove);
		}
	}	
	
	// We are in jump mode but just became grounded
	if (IsGrounded())
	{
		lastGroundedTime = Time.time;
		inAirVelocity = Vector3.zero;
		if (jumping)
		{
			jumping = false;
			SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
		}
	}
}

function OnControllerColliderHit (hit : ControllerColliderHit )
{
//	Debug.DrawRay(hit.point, hit.normal);
	if (hit.moveDirection.y > 0.01) 
		return;
}

function GetSpeed () {
	return moveSpeed;
}

function IsJumping () {
	return jumping;
}

function IsGrounded () {
	return (collisionFlags  CollisionFlags.CollidedBelow) != 0;
}

function GetDirection () {
	return moveDirection;
}

function IsMovingBackwards () {
	return movingBack;
}

function GetLockCameraTimer () 
{
	return lockCameraTimer;
}

function IsMoving ()  : boolean
{
	return Mathf.Abs(Input.GetAxisRaw("Vertical")) + Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5;
}

function HasJumpReachedApex ()
{
	return jumpingReachedApex;
}

function IsGroundedWithTimeout ()
{
	return lastGroundedTime + groundedTimeout > Time.time;
}

function Reset ()
{
	gameObject.tag = "Player";
}

The only problem; back and forward, left and right turns, smooth rotation …