- Home /
why does my character passes through trees ??
i know that this question has been asked multiple times...so sorry, and i even saw all the solutions over the net. but nothing worked out for me.. i have a terrain, a tree and a character. howerver, the chararcter passes through the tree. i have applied capsule collider to both of them...but still nothing works... :( can anyone pls say how can i resolve this problem.. its very annoying... thnx in advance..
Answer by yuvaansh · Nov 13, 2020 at 12:23 PM
I think that the collider is marked as trigger. If it is not set as trigger then I think that your "Player" has a Rigidbody component attached to it. So, I think that you are using transform to move your Player instead of that use rigidbody.position.
I hope this will answer your question ;^)
the character doesn't has a RigidBody, and there is a line of code which moves it forward..
transform.Translate(Vector3.forward * (speed) * Time.deltaTime);
Should i change it?? if so... can u pls tell me what should i write...coz i am new to unity.
@swastika_pallai
$$anonymous$$aybe a public property is there in the code but it is not assigned. So, the code after that sound is used will not run maybe that's why it won't collide.
I hope this would help 8D
can u say what should i do??? and can u say what does that mean?? i am very new to unity here i have shared the screenshot of the inspector of the tree and the player... player:-
and the tree:-
Answer by yuvaansh · Nov 13, 2020 at 01:40 PM
@swastika_pallai
Now I understand what's the problem. If anything is colliding with other object one of those objects should have a Rigidbody component so you can add the component and configure it. I hope now the bug is fixed ;-D
now i have added a mesh collider, capsule collider and a terrain collider.. and i have sent a screenshot of what is happening...
and after editting, the inspector of bith the character and the tree is like this:-
after looking at its result, i think there might be some issues in the code.. so i have pasted them too... there are two files, one for movement and one for the camera..
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camera_controller : $$anonymous$$onoBehaviour
{
public enum RotationAxis
{
$$anonymous$$ouseX = 1,
$$anonymous$$ouseY = 2
}
public RotationAxis axes = RotationAxis.$$anonymous$$ouseX;
public float $$anonymous$$imumVert = -5.0f;
public float maximumVert = 35.0f;
public float sensHorizontal = 10.0f;
public float sensVertical = 10.0f;
public float _rotationX = 0;
// Update is called once per frame
void Update()
{
if (axes == RotationAxis.$$anonymous$$ouseX)
{
transform.Rotate(0, Input.GetAxis("$$anonymous$$ouse X") * sensHorizontal, 0);
}
else if (axes == RotationAxis.$$anonymous$$ouseY)
{
_rotationX -= Input.GetAxis("$$anonymous$$ouse Y") * sensVertical;
_rotationX = $$anonymous$$athf.Clamp(_rotationX, $$anonymous$$imumVert, maximumVert); //Clamps the vertical angle within the $$anonymous$$ and max limits (45 degrees)
float rotationY = transform.localEulerAngles.y;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
}
}
and
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : $$anonymous$$onoBehaviour
{
public float speed = 100.0f;
private CharacterController _charCont;
// Use this for initialization
void Start()
{
_charCont = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.forward * (speed) * Time.deltaTime);
}
}
Answer by President · Nov 13, 2020 at 01:43 PM
If you are not using a Rigid Body for you your Character, then it needs to have "Character Controller", that's the first thing.
Second thing, I can see that the Height in your tree Capsule Collider is 0, that is wrong too. The collider should cover the height of the tree. However, a better way to do it to add mesh collider to your tree instead of capsulate collider.
now i have added a mesh collider, capsule collider and a terrain collider.. and i have sent a screenshot of what is happening...
and after editting, the inspector of bith the character and the tree is like this:-
after looking at its result, i think there might be some issues in the code.. so i have pasted them too... there are two files, one for movement and one for the camera..
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camera_controller : $$anonymous$$onoBehaviour
{
public enum RotationAxis
{
$$anonymous$$ouseX = 1,
$$anonymous$$ouseY = 2
}
public RotationAxis axes = RotationAxis.$$anonymous$$ouseX;
public float $$anonymous$$imumVert = -5.0f;
public float maximumVert = 35.0f;
public float sensHorizontal = 10.0f;
public float sensVertical = 10.0f;
public float _rotationX = 0;
// Update is called once per frame
void Update()
{
if (axes == RotationAxis.$$anonymous$$ouseX)
{
transform.Rotate(0, Input.GetAxis("$$anonymous$$ouse X") * sensHorizontal, 0);
}
else if (axes == RotationAxis.$$anonymous$$ouseY)
{
_rotationX -= Input.GetAxis("$$anonymous$$ouse Y") * sensVertical;
_rotationX = $$anonymous$$athf.Clamp(_rotationX, $$anonymous$$imumVert, maximumVert); //Clamps the vertical angle within the $$anonymous$$ and max limits (45 degrees)
float rotationY = transform.localEulerAngles.y;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
}
}
and
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : $$anonymous$$onoBehaviour
{
public float speed = 100.0f;
private CharacterController _charCont;
// Use this for initialization
void Start()
{
_charCont = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.forward * (speed) * Time.deltaTime);
}
}
You don't need 2 colliders, one will do the job. Either "$$anonymous$$esh Collider" or "Capsule Collider". Let me break it down for you (The better way). Looking at your "$$anonymous$$esh Collider", your "$$anonymous$$esh" says "None ($$anonymous$$esh)". That means you don't have a mesh identified, and your mesh collider will do nothing, as if it doesn't exist. So if your tree has a mesh, you need to put it in the "$$anonymous$$esh" field, if it doesn't, I suggest that you find or create another tree that has a mesh collider.
now i have added a mesh but still.... here's a screenshot..
and here i have shared the full inspector of the tree and the player..
alt text
Answer by yuvaansh · Nov 13, 2020 at 02:33 PM
@swastika_pallai On the mesh collider check the convex option. That will inform the mesh collider to take the most possible shape of that object.
Your answer
Follow this Question
Related Questions
Tilemap Collider 2D 2 Answers
Unity 5 - Cloth falling through floor 0 Answers
Mesh Collider didn't work unless i walk slowly towards it 1 Answer
Climbing using OnTriggerEnter and Arrow Keys? 1 Answer
Problems with raycast accuracy 1 Answer