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 Keringar · Jul 13, 2014 at 05:10 PM · 2dstuckspherecast

Sphere casting a wall always returns false?

Hello, My 2d platformer character keeps on getting stuck on the walls while in midair. I tried to solve this by setting player velocity to zero upon a sphere cast hitting the wall. However I am new to ray casting and I can't seem to be able to get the sphere cast to return true.

I have a game object called frontSide that I draw a sphere cast from the transform.position of my player. If anyone could help me figure out why my sphere cast always returns false that would be greatly appreciated.

Code:`using UnityEngine; using System.Collections;

public class PlayerMovement : MonoBehaviour {

 public bool facingRight = true;
 public bool jump = false;

 public float horizVel = 0f;
 static Vector3 mirrorX = new Vector3(-1,1,1);

 public float moveForce = 300f;
 public float maxSpeed = 10f;
 public float jumpForce = 1000f;

 private Animator animator;
 private bool grounded = false;
 private Transform groundCheck;
 private Transform frontSide;

 private bool sideCheckLeft;
 private bool sideCheckRight;

 private RaycastHit hitL;
 private RaycastHit hitR;

 // Use this for initialization
 void Awake () {
     animator = GetComponent<Animator>();
     groundCheck = transform.Find ("groundCheck");
     frontSide = transform.Find ("frontSide");
 }
 
 // Update is called once per frame
 void Update () {
     grounded = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer ("ground"));
     sideCheckLeft = Physics.SphereCast (transform.position, 2.0f , frontSide.position, out hitL, 100, 1 << LayerMask.NameToLayer ("ground"));
     //sideCheckRight = Physics.SphereCast (transform.position, 2.0f, Vector3.right, out hitR, 2, 1 << LayerMask.NameToLayer ("ground"));

     if (Input.GetButtonDown ("Jump") && grounded)
         jump = true;

     print (sideCheckLeft);

     if (sideCheckLeft)
 //        rigidbody2D.velocity.x = 0f;
         print ("hit");

     Debug.DrawLine(transform.position, frontSide.position, Color.red);

 }

 void FixedUpdate (){
     float h = Input.GetAxis ("Horizontal");

// animator.SetFloat ("Speed", Mathf.Abs (h));

     horizVel = Mathf.Clamp(h*(moveForce/rigidbody2D.mass)*Time.deltaTime, -maxSpeed, maxSpeed);

     Vector3 vel = rigidbody2D.velocity;
     vel.x = horizVel;
     rigidbody2D.velocity = vel;

     if ( (h<-0.01f && facingRight) || (h>0.01f && !facingRight) ){
         transform.localScale = Vector3.Scale(transform.localScale, mirrorX);
         facingRight = !facingRight;
     }

     if(jump)
        rigidbody2D.AddForce(new Vector2(0f, jumpForce));
        jump = false;
 }

 void Flip()
 {
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;

     facingRight = !facingRight;
 }

}`

This is a picture of my setup: http://imgur.com/kNRiaPW

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 Pyrian · Jul 13, 2014 at 07:03 PM

You want Physics2D.CircleCast, not Physics.SphereCast.

Physics (which is 3D) and Physics2D do not interact. SphereCast does not find any 2D colliders, nor does CircleCast find any 3D colliders.

Comment
Add comment · Show 1 · 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 Keringar · Jul 13, 2014 at 08:25 PM 0
Share

Thanks for helping a noob.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Animator trigger stuck 4 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

CharacterController sinking on edges of colliders 0 Answers

resizing spherecast radius over distance? 4 Answers

New to Unity and stuck!!! 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