- Home /
Object hover over collider during runtime?
So I have a ball that acts as a controller for my "blob" character, its strange though because when I press play, the collider seems to hit the ground, but my blob character seems to hover above the center. Any ideas on fixing this? Ill post my code and some images. Thank you!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerAnimation : MonoBehaviour { public GameObject player; Animator animator;
// Start is called before the first frame update
void Start()
{
animator = this.gameObject.GetComponent<Animator>();
}
private void Update()
{
player = GameObject.Find("Player");
if ((Input.GetAxis("Horizontal") != 0) || (Input.GetAxis("Vertical") != 0))
{
animator.SetBool("Moved", true);
Vector3 newPosition = new Vector3(Input.GetAxis("Vertical"), 0.0f, -Input.GetAxis("Horizontal"));
transform.LookAt(newPosition + transform.position);
transform.Translate(-newPosition * 10f * Time.deltaTime, Space.World);
}
else if ((Input.GetAxis("Horizontal") == 0) || (Input.GetAxis("Vertical") == 0)) { animator.SetBool("Moved", false); }
}
void LateUpdate()
{
transform.position = player.transform.position;
}
} In Editor: During Runtime:
@3kWikiGames How is your visual character connected to the (presumably) rigidbody? If they are unconnected, the rigidbody blob would fall due to gravity, while the (non rigidbody) character would remain where it was.
@JPhilipp So I have the separate ball (that I use to control the movements) and that has a rigidbody but the blob character, which is separate does not. Its connected by the late update at the bottom that basically copies the location of the "player". I'm sure I made this a lot more difficult than I needed to.
Player is the Controller containing the collider and rigidbody, Blob obj is the visual representation that doesnt have anything.
Answer by 3kWikiGames · Mar 14, 2020 at 11:29 PM
Ended up changing my lateupdate method to this, now it works fine:
void LateUpdate()
{
Vector3 pos = new Vector3(player.transform.position.x,player.transform.position.y-.39f,player.transform.position.z);
transform.position = pos;
}
Answer by Link17x · Mar 14, 2020 at 09:01 PM
It looks like you could have a Rigidbody component that has "Gravity" ticked in the inspector.
The other reason could be that if you are playing an animation for the movement, check the Y-axis of the animation clip. It could have been recorded with a Y-axis value of 0, meaning that when you play the animation, it resets your gameObject Y-axis back to 0.
Neither of my animations affect the y-axis position only the scale, when I turned animations off it still was doing it so Im assu$$anonymous$$g it has something to do with my code?
Do you have a collider on your 'blob' as well as your ball? If so, it could be that when your ball collider hits the ground, the 'blob' collider starts above the ball, meaning that the ground collider will push it up (assu$$anonymous$$g they are not triggers).
I'm not sure the set-up, but the other thing to double-check is that you are setting your positions to what you think you are. Use a couple of Debug.Log's to print the position and make sure it is what you expect it to be. You are setting the position based on the Input.Axis, you could print the value from this to see if it is the value you expect too.
@leeokeefe I think I understand the problem now, it seems that since my script is putting the blob obj at the same position as the ball its setting it to 0,0,0 but because of this I need both positions to be set up properly already at 0,0,0. Any ideas on how to do this?
Your answer
![](https://koobas.hobune.stream/wayback/20220612224946im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Mesh collider not working correctly on voxel terrain 1 Answer
Should I load all the database items on my android device? 1 Answer
Runtime mesh decimation 1 Answer