- Home /
Third Person Controller. Need Help
Hi! I'm new to scripting and trying to create third person controller script for my character alike in Resident Evil (1,2,3) games or something like this. I'm using a few methods I've already learned to control my character like moving forward and backward with "w" and "s" buttons f.e. and rotating with "a" and "d" just like in RE. Now I'm brainstorming a few problems: I wonder how can I make my character sprint with let's say "shift" button pressed but only when moving forward. Also I need targeting action with f.e. "ctrl" button pressed and the ability to rotate my character when targeting. Here's a code:
@script RequireComponent(CharacterController)
public var idleAnimation : AnimationClip;
public var walkAnimation : AnimationClip;
public var runAnimation : AnimationClip;
public var shootAnimation : AnimationClip;
public var walkMaxAnimationSpeed : float = 0.75;
public var runMaxAnimationSpeed : float = 1.0;
public var shootAnimationSpeed : float = 1.15;
private var _animation : Animation;
private var _characterState : CharacterState;
var walkSpeed = 2.0;
var runSpeed = 6.0;
var rotateSpeed = 3.0;
//shooting
var Fireball:Transform;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
animation.wrapMode = WrapMode.Loop;
if (Input.GetAxis("Vertical") > 0.2)
animation.CrossFade ("walk");
else
animation.CrossFade ("idle");
if
(Input.GetAxis("Vertical") < 0.0)
animation.CrossFade ("walk");
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = walkSpeed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetButtonDown("Jump"))
{
var bullit = Instantiate(Fireball, gameObject.Find("fireballspawn").transform.position,Quaternion.identity);
bullit.tag = "PlayerProjectile";
bullit.rigidbody.AddForce(transform.forward * 2000);
}
}
@script RequireComponent(CharacterController)
There's a rough copy of shooting action for the character but it's not finished and even tested yet. Thank's in advance for your help!
Answer by deltamish · Feb 23, 2013 at 02:32 PM
Hi To make your character run its just as simple as above
Here's a snippet
var move:Vector3;
//ommited all others
function Update(){
//ommited all others
if (controller.isGrounded && Input.GetButton("Sprint") && !crouch){
forward = new Vector3(0,0,Input.GetAxis("Vertical"));
forward = transform.TransformDirection(move);
forward *= runSpeed;
}
controller.Move(forward * Time.deltaTime);
}
And by targetting system do you mean to raise the character's hand or make the character to point at something automatically.please do tell more about it
Update hi,here is the aim script it a basic one all it does is it makes the hand to go to aim position if you press fire if not it comes back (if you want a more advanced one that keeps it in that position for some time and then lowers it then please do ask But i wont be able to post it quickly cause i kinda busy with my exams sorry)
var Aim_Anim:AnimationClip;
var RUpperArm:Transform;
function Start(){
animation[Aim_Anim.name].layer = 1;
animation[animation[Aim_Anim.name].wrapMode = WrapMode.ClampForEver;
}
function Update(){
//in your other code ie run or walk anim add animation[Aim_Anim.name].RemoveMixingTransform(RUpperArm);
if(Input.GetButton("Fire1")){
animation[Aim_Anim.name].speed = 1;
animation[Aim_Anim.name].AddMixingTransform(RUpperArm);
animation.CrossFade(Aim_Anim.name);//you can also use animation.Play
}else{
animation[Aim_Anim.name].AddMixingTransform(RUpperArm);
animation[Aim_Anim.name].speed = -1;
animation.CrossFade(Aim_Anim.name);//you can also use Play
}
}
Thank you so much for the answer! This script should have worked well, but when I've implemented it into my code a character started moving forward before I pressed any key. But he stops moving when I'm holding "shift" button and moves slower than in state when none of the keys are pressed. However I've already found a decision by applying this ins$$anonymous$$d:
if (Input.GetButton ("Shift"))
var runSpeed = runSpeed * Input.GetAxis ("Vertical");
controller.Simple$$anonymous$$ove(forward * runSpeed)
It seem to work well, but now while holding "shift" character keeps running both forward and backward directions. I guess I need to add some exclusion here, just like when move back while holding "shift", he could not turn into run? As for targeting it would be nice to know both (character's hands raising and make him point automatically):D But what I really meant was to get into the position/state, from which only, a character could start shooting. And he should keep his hands raised while rotating also. I guess I'll have to use kinda concept for this when several animations played at once.
Well, I've finally done with that walk/run problem implementing a few extra code lines! Nonetheless I'll appreciate any help with ai$$anonymous$$g and shooting tasks :)
Hi @Sirmaiji do have an animation or somthing for the hand to got aim position
Hi! As for the ai$$anonymous$$g animation, work's still in progress, but it's gonna be alike in RE series. F.e. character holds a pistol with his two hands. I guess I'll need to somehow pause the last frame of that animation (when the hands reach right position) and hold it while button's pressed. When I release the button animation should go back to 0 frame (I guess "Ping Pong" playing animation option or something like this might be of use here) But I'm getting stuck again furthermore with some other problems such as how to make raise character's hands or move em down when ai$$anonymous$$g up or down? As you can see so many questions and objectives I've got...
Hi allow you got do is animation that make hand go to aim position
then attach the animation to the script i posted ,the script will make the arm automatically to toggle between aim position that is it automatically goes to aim pos when you press fire and when not it goes back to original pos and plays the walk anim or idle anim
to make the hand point where your are targetting i am kinda busy to write the script so why dont you take a look at Headlook controller(Google it)
Your answer
Follow this Question
Related Questions
How would I make a 3rd person Controller that only moves up, down, left, and right? 2 Answers
Alan Wake style 3rd person character controls, help! 1 Answer
Unity Jet Wing Animations! Help! 1 Answer
Declaring animations on a script in the editor 1 Answer
loop an animation if the button witch activates it is still pressed 0 Answers