Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sirmaiji · Feb 23, 2013 at 01:59 PM · animationsthird-person3rd person controller

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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
 }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sirmaiji · Feb 23, 2013 at 04:04 PM 0
Share

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.

avatar image Sirmaiji · Feb 23, 2013 at 05:57 PM 0
Share

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 :)

avatar image deltamish · Feb 24, 2013 at 05:14 AM 0
Share

Hi @Sirmaiji do have an animation or somthing for the hand to got aim position

avatar image Sirmaiji · Feb 24, 2013 at 12:13 PM 0
Share

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...

avatar image deltamish · Feb 25, 2013 at 08:31 AM 0
Share

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)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges