Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ChillCo · Jan 10 at 09:45 AM · physicscamera-movementmovement script

need help fixing third person character controller

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class characterMovement : MonoBehaviour
 {
     //variables and functions
     public float moveSpeed;
     bool attackcooldown = true;
     public float health = 100f;
     float oldhealth = 100f;
     public bool dead = false;
     bool canheal = true;
     Vector3 moveDirection;
     void Start()
     { 
         moveSpeed = 1f;
         health = 100;
         dead = false;
     }
     void FixedUpdate()
     {
         //movement
         if (dead == false)
         {
             if (new Vector3(Input.GetAxisRaw("Vertical"), 0, Input.GetAxisRaw("Horizontal")).magnitude != 0) 
             {
                 gameObject.GetComponent<Rigidbody>().AddForce(moveDirection * moveSpeed, ForceMode.Acceleration) ;
             }
         }
 
     }
     //update loop
     void Update()
     {
         //moveDirection updating
         moveDirection = (GameObject.Find("Camera Holder").transform.GetChild(0).transform.forward * Input.GetAxisRaw("Vertical") + GameObject.Find("Camera Holder").transform.GetChild(0).transform.right * Input.GetAxisRaw("Horizontal")).normalized;
         moveDirection = new Vector3(moveDirection.x, 0, moveDirection.z);
         //death
         if (health < 1)
         {
             if (dead == false)
             {
                 dead = true;
             }
         }
         //attacking
         //if (Input.GetKeyDown(KeyCode.Mouse0) && attackcooldown && dead == false)
         //{
             //StartCoroutine(waiter());
             //IEnumerator waiter()
             //{
                 //GameObject.Find("Main Camera").transform.GetChild(0).transform.GetChild(0).GetComponent<Animation>().Play();
                 //attackcooldown = false;
                // yield return new WaitForSeconds(0.1f);
                 //GameObject.Find("Main Camera").transform.GetChild(0).transform.GetChild(0).transform.GetChild(0).GetComponent<TrailRenderer>().emitting = true;
                 //yield return new WaitForSeconds(0.3f);
                 //RaycastHit hitinfo;
                 //if (Physics.Raycast(GameObject.Find("Main Camera").transform.position, GameObject.Find("Main Camera").transform.forward, out hitinfo, 2f))
                 //{
                     //GameObject clone = Instantiate(GameObject.Find("hiteffectsone"), hitinfo.point, Quaternion.Euler(0, 0, 0));
                     //GameObject clonetwo = Instantiate(GameObject.Find("hiteffectstwo"), hitinfo.point, Quaternion.Euler(0, 0, 0));
                     //clone.GetComponent<ParticleSystemRenderer>().material = hitinfo.transform.gameObject.GetComponent<Renderer>().material;
                     //clone.GetComponent<ParticleSystem>().Emit(15);
                     //clonetwo.GetComponent<ParticleSystem>().Emit(10);
                     //Destroy(clone, 10);
                     //Destroy(clonetwo, 10);
                 //}
                 //yield return new WaitForSeconds(0.1f);
                 //GameObject.Find("Main Camera").transform.GetChild(0).transform.GetChild(0).transform.GetChild(0).GetComponent<TrailRenderer>().emitting = false;
                 //yield return new WaitForSeconds(GameObject.Find("Main Camera").transform.GetChild(0).transform.GetChild(0).GetComponent<Animation>().clip.length - 0.5f);
                // attackcooldown = true;
            // }
         //}
         if (Input.GetKeyDown(KeyCode.LeftShift) && dead == false)
         {
             moveSpeed = 45f;
         }
         if (Input.GetKeyUp(KeyCode.LeftShift) && dead == false)
         {
             moveSpeed = 30f;
         }
         if (new Vector3(Input.GetAxisRaw("Vertical"), 0, Input.GetAxisRaw("Horizontal")).magnitude != 0)
         {
             transform.GetChild(0).transform.LookAt(transform.GetChild(0).transform.position + moveDirection);
         }
             
         if (canheal == true && dead == false)
         {
             StartCoroutine(healing());
             IEnumerator healing()
             {
                 canheal = false;
                 if (health < 100)
                 {
                     health = health + 1;
                 }
                 yield return new WaitForSeconds(0.5f);
                 canheal = true;
             }
         }
     }
 }

i need help with the actual rigidbody movement of this script, note: PLEASE IGNORE ALL THE STUFF THAT IS NOT RELATED TO RIGIDBODY MOVEMENT, THAT IS ALL STUFF PLANNED FOR WHEN I GET THIS DONE

my issue here is that the rigidbody constantly moves left relative to the camera, various things ive tried are: -changing ways i calculate the movedirection variable -switching from velocity to addforce -messing around with physics material stats -changing my hierarchy from rotating the entire capsule to rotating just a mesh - and lastly, changing movespeed values and cutting out a camera bob system out of fear it would tamper with the movedirection variable (its all relative to the camera so if the camera is moving i figured it would get screwed up)

few things to note: the rotation of the mesh works fine, it turns left relative to the camera when i hit a, right when i hit d, forward when i hit w, and so on, so i believe its an issue with physics and not the movedirection variable

feel free to ask for any additional information, dont want to clutter my post too much though right off the bat

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
0

Answer by ChillCo · Jan 10 at 11:09 PM

I SOLVED IT, turns out it was a matter of rigidbody settings and using moveposition, so i was half right on that!

edit: nope, by setting rigidbody to be kinematic i could psuedo-solve the issue temporarily, but then i realized collision didnt work, when i turned it off it just crumbled again, please i am desperate to get this bare bones third person character controller working, please!

Comment
Add comment · 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

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

220 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Non slippery movement 1 Answer

Camera smooth follow jitters 0 Answers

2D 360 degress platformer example needed 0 Answers

Manual Player Physics Help (C#) 0 Answers

Quake Movement in Unity 1 Answer


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