Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Dec 22, 2016 at 05:41 PM by Nekoluffy.
avatar image
0
Question by Nekoluffy · Dec 21, 2016 at 04:26 PM · raycastmovement scriptcharacter controller

Infamous first light run system, problem with my code, HELP

Hey guys! c: Im a noob programmer, ive been trying to program for years. ive only just gotten the hang of it however. took me a while to rack my brain around concepts and stuff and i lacked he focus to put in the time. Im starting to get there now.

I created this movement script from scratch. Basically. if you hold the Z key and run into a wall, you can run up the wall. if you just in the air, you jump via the transform up direction no matter what wall youre on. If youre not grounded then your rotation resets to the world rotation.

Im trying to create a run system like Infamous first light. But starting in 2D so its a little easier first.

Heres whats messng me up. I cant get the raycast to work on the lefthand side to read for a wall on the left. And it might have something to do with the rotation, but I cant figure out what it is exactly. And when i jump in the air the rotation is always facing right. Also when running along the wall, sometimes when letting go of the Z key, it doesnt do what ts supposed to do and get off the wall, and change back rotations. Sometimes it just sticks to the wall as if youre still holding it.

Thanks for the help in advance, ill keep racking my brain around it and trying to rework the code in the mean time. PS: Ignore some of the unused variables such as lineRenderer, i use it to test the length of raycast and such.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     public bool grounded;
     public bool facingRight;
 
     Vector3 playerUpVector;
     Vector3 originalUpVec;
     Quaternion originalRot;
     Vector3 surfaceNormal;
 
     Rigidbody rb;
     Renderer rend;
     LineRenderer[] lineRends;
 
     float gravity = 10;
     float walkSpeed = 10;
 
 
 
     // Use this for initialization
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         rend = GetComponent<Renderer>();
         playerUpVector = transform.up;
         originalUpVec = transform.up;
         rb.freezeRotation = true;
         lineRends = GetComponentsInChildren<LineRenderer>();
         originalRot = transform.rotation;
         facingRight = true;
 
     }
 
     // Update is called once per frame
 
     void Update()
     {
         float h = Input.GetAxis("Horizontal");
         Vector3 movement = transform.right * h * Time.deltaTime * walkSpeed;
         
 
         if (movement.magnitude > 0)
         {
             if (h < 0)
             {
                 facingRight = false;
                 transform.rotation = Quaternion.Euler(0, 180, 0);
                 movement = transform.right * -h * Time.deltaTime * walkSpeed;
                 rb.MovePosition(rb.position + movement);
             }
             else if (h > 0)
             {
                 facingRight = true;
                 transform.rotation = Quaternion.Euler(0, 0, 0);
                 rb.MovePosition(rb.position + movement);
             }
             
         }
         Debug.DrawRay(new Vector3(rend.bounds.extents.x,rend.bounds.center.y,0), new Vector3(rend.bounds.center.x - (rend.bounds.extents.x/2), -rend.bounds.extents.y - .4f,0),Color.red);
 
         
     }
 
     void FixedUpdate()
     {
         if (grounded && transform.up != Vector3.up)
         { 
         rb.AddForce(-gravity * rb.mass * playerUpVector);
         }
         if (grounded && Input.GetKeyDown(KeyCode.Space))
         {
             rb.AddForce(transform.up * 500 );
         }
 
 
         RaycastHit hit;
 
         if (Input.GetKey(KeyCode.Z))
         {
             if (facingRight && Physics.Raycast(transform.position, transform.right, out hit, .501f))
             {
                 surfaceNormal = hit.normal;
 
                 Quaternion newRot = Quaternion.LookRotation(transform.forward, surfaceNormal);
                 transform.rotation = newRot;
                 rb.useGravity = false;
                 playerUpVector = hit.normal;
             }
             if (!facingRight && Physics.Raycast(transform.position, transform.right, out hit, .51f))
             {
                 surfaceNormal = hit.normal;
 
                 Quaternion newRot = Quaternion.LookRotation(transform.forward, surfaceNormal);
                 transform.rotation = newRot;
                 rb.useGravity = false;
                 playerUpVector = hit.normal;
             }
 
         }
 
         if (Physics.Raycast(transform.position, -transform.up, out hit, .51f))
         {
             grounded = true;
         }
         else
         {
             grounded = false;
         }
 
         if (Input.GetKeyUp(KeyCode.Z) || !grounded)
         {
             rb.useGravity = true;
             transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y,originalRot.z);
             playerUpVector = originalUpVec;
             
         }
 
     }
 }
 
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Continuously rotate a gameobject on 1 axis perpendicularly to another gameobject's normal? 1 Answer

Creating custom character controller collision checking 0 Answers

How to make a 3rd person character controller 0 Answers

How to add platform motion to player controller without parenting? - Mostly working code 1 Answer

Character controller air control 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